Auto Start Python Script on Boot (Ubuntu 20.04, Systemd)

Create a Systemd Entry

sudo nano /etc/systemd/system/myapp.service
# After=network.service
Description=My App

# WorkingDirectory=/code/python/myapp
# User=do-user


NOTE: Use After=network.service if you require network.

Create Bash Script

nano /code/scripts/
#!/bin/bash# cd /code/python/myapppython3 /code/python/myapp/ >> /code/logs/myapp.log 2>&1
mkdir /code/logs

Create Python Script

nano /code/python/myapp/
import signalimport timeimport datetimeis_shutdown = Falsedef stop(sig, frame):  print(f"SIGTERM at {}")  global is_shutdown  is_shutdown = Truedef ignore(sig, frsma):  print(f"SIGHUP at {}")signal.signal(signal.SIGTERM, stop)signal.signal(signal.SIGHUP, ignore)print(f"START at {}")while not is_shutdown:  print('.', end='', flush=True)  time.sleep(1)print(f"END at {}")

Test Systemd

Set file permission

sudo chmod 744 /code/scripts/myapp.shsudo chmod 664 /etc/systemd/system/myapp.service

Enable Service

sudo systemctl daemon-reloadsudo systemctl enable myapp.service

Test Service

sudo systemctl start myapp.service

Check Status

sudo systemctl start status.service

Check Log

tail -f /code/logs/myapp.log

Stop Service

sudo systemctl stop myapp.service

Restart server to test if the service started on reboot

sudo reboot


Systemd is run as root by default. If you are using Anaconda (or maybe pyenv), current user might not run the same python version/environment as root.

NOTE: I tried changing the user via Service.User, but it still uses the system default python interpreter

python -Vsudo python -V

You can find the python interpreter path using which python or python -c "import sys; print(sys.executable)"

which pythonsudo which python

Edit the bash script to use the correct python interpreter

/opt/conda/bin/python3 /code/python/myapp/ >> /code/logs/myapp.log 2>&1

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.