Run Python Script At Startup in Ubuntu

Start a python script at startup automatically?

1) Don't use old "init.d" method. Use something modern. If you have Ubuntu 15.04 and higher, you can use Systemd to create daemon that will be started automatically at startup. If you have for example Ubuntu older than 15.04 - use Upstart.

For Systemd:

Create unit file in /lib/systemd/system/you_service_name.service with the following content (as far as I can see your python script doesn't spawn new process while running, so Type should be simple. More info here):

[Unit]
Description=<your_service_name>
After=network.target network-online.target

[Service]
Type=simple
User=<required_user_name>
Group=<required_group_name>
Restart=always
ExecStartPre=/bin/mkdir -p /var/run/<your_service_name>
PIDFile=/var/run/<your_service_name>/service.pid
ExecStart=/path/to/python_executable /path/to/your/script.py

[Install]
WantedBy=multi-user.target

Save this file and reload systemd:

sudo systemctl daemon-reload

Then add your service to autostart:

sudo systemctl enable you_service_name.service

you should see that Systemd created required symlinks after enable action.

Reboot and see if it's up and running (ps aux | grep python or sudo systemctl status you_service_name.service). If there is something weird - check Systemd journal:

sudo journalctl -xe

UPD:

To launch your python script in desired virtualenv, just use this expression in your service unit file:

ExecStart=/venv_home/path/to/python /venv_home/path/to/your/script.py

2) You can also use crontab, but you need to specify full path for desired shell there, for example:

@reboot /bin/bash /path/to/script.sh

If you need additional help - just let me know here.

Run Python Script on OS boot

Place the script into /etc/rc.local . Scripts there run as root when the system starts. It is also suitable for Raspberry Pi, as you specified in the comments.

In your case you want to run it as python /path/to/script.py &

Here's my sample rc.local file, I use the same approach to run battery and temperature monitoring scripts

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/home/xieerqi/bin/batmon.sh &
/home/xieerqi/bin/preventShutdown.sh &
/home/xieerqi/bin/sh/temperature.sh &

Additional info on /etc/rc.local

Run Python script on startup as root

The easiest way is to create a systemd service that might look like:

[Unit]
Description=Some python script
After=network.target

[Service]
ExecStart=/usr/bin/python3 script.py
WorkingDirectory=/path/to/scriptdir
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root

[Install]
WantedBy=multi-user.target

You should save this in /etc/systemd/system/servicename.service where servicename can be anything, then set it to run on startup with sudo systemctl enable servicename.service.

Autostarting a python script on Ubuntu

I faced the same problem when I run PyPI local server.
I developed a local PyPI server and it should be run using hostingpypi runserver --port PORT --host HOST command.

So, borrowed systemd config file from https://pypi.org/project/pypiserver/#running-as-a-systemd-service and used it for running my script at Ubuntu startup.



Related Topics



Leave a reply



Submit