How to run a python program at startup (systemd)

So you've created a program in python and now you want it to run at boot, here's a quick rundown on how.

There are a few ways to do this, but I find using systemd preferable. It's easier to get module imports working and can even be used for things like running at specific times.
Create a systemd unit file with .service extension in /etc/systemd/system, for example /etc/systemd/system/discord_bot.service, with the following content:

[Unit]
Description=Service description

[Service]
WorkingDirectory=/home/ryan/discord_bot/ # Directory to start in
ExecStart=/usr/bin/python3 /home/ryan/discord_bot/bot.py
User=ryan # User to run as
Restart=always # Restart if it closes

[Install]
WantedBy=multi-user.target

After this, assuming that your python file exists, you should be able to start the service with: systemctl start discord_bot. To enable this to startup at boot use: systemctl enable discord_bot.

Explanation

The main takeaways here the code snippets: ExecStart=/usr/bin/python3 /home/ryan/discord_bot/bot.py and WantedBy=multi-user.target. The first of these simply tells the service what to execute when started, similar paramaters exist such as ExecReload and ExecStop. The second one, WantedBy=multi-user.target, effectively waits until the system is ready to start doing stuff a normal user would do. This means that the network services are ready e.t.c. For this example, User=ryan is extremely important. This runs the python program under my user, meaning the imports work.

Mastodon