Occasionally, I find myself wanting to start something custom as a systemd service, so it starts at boot. There’s a whole wealth of information on how to properly write systemd services, but I just want the bare minimum to get my command executed on boot and running on its own. Hence, here is the most basic systemd service guide. Feel free to read the systemd docs (systemd.service, systemd.unit, systemd.exec) for more info on what can go in the service file if you want to get fancy.

Start by determining the full path to your service. If it’s on the PATH, you can do this with which. If you wrote it and it doesn’t install itself in /usr/local/bin like a good program, it might be in your home folder. If it’s a script and needs an interpreter, you need the full path to the interpreter followed by the full path to the script

Second, create a systemd service file in the right place and edit it.

sudo touch /etc/systemd/system/myservice.service
sudo chmod 664 /etc/systemd/system/myservice.service
sudo nano /etc/systemd/system/myservice.service

Now, write out the shortest possible configuration file including the full path and command line to execute your service

[Unit]
Description=A Cool Service, Wow!
After=network-online.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/myservice/main.py -a -b -c -d
Restart=always

[Install]
WantedBy=multi-user.target

Finally, tell systemd to reload it’s daemon files (since we modified them), then start the service (launch it now), then enable the service (set it to launch automatically on boot).

sudo systemctl daemon-reload
sudo systemctl start myservice
sudo systemctl enable myservice

You can also view the latest bit of the log to check for any errors

sudo systemctl status myservice

The End! Systemd is quite easy to work with, and using it to launch your scripts at startup is handy.