Following the excellent blog post by Brian Starkey @usedbytes to set up my robot programs to launch on start-up on a Raspberry Pi, I found several things which didn't work with my particular programs. This article aims to extend the information from Brian's post.
First a quick recap on why use systemd services instead of rc.local or autostart desktop files. For me the main advantages of this method are:
- The automatic start up of the robot code on boot can be turned on and off, which really helps when developing code and you don't want the robot program to launch each time you boot up.
- You can configure the code to be restarted if it stops for any reason. Great for if an unexpected bug crashes your robot code in the middle of a demo or competition. No need to wait for a reboot to get the code running again.
[Unit]Description=Robot Runner[Service]Type=execExecStart=/usr/bin/python3 /home/pi/pygame-controller/examples/SentinelBoard/Turbo4WD.pyUser=piEnvironment="DISPLAY=:0"Environment="XAUTHORITY=/home/pi/.Xauthority"Restart=alwaysRestartSec=5s[Install]WantedBy=graphical.target
Command Quick Reference
sudo cp runrobot.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable runrobot
sudo systemctl disable runrobot
sudo systemctl start runrobot
sudo systemctl stop runrobot
sudo journalctl -b0 --unit=runrobot