I have been getting reports lately where crontab didn’t work for some people, so I thought I’d show you a quick way to using systemd instead of crontab fixing the issue of your image viewer not starting at boot. I will use my favorite image viewer Pi3D as an example.
A quick introduction on systemd
systemd is a system and service manager for Debian, the Linux variant at the core of Raspbian. With narrow regard to our use case, this means that it can manage user processes. Its job is to “quickly solve problems”. In coding, that message has a nice ring to it!
The great thing about using systemd is that it can monitor processes.
So whereas crontab can launch your program or script at boot, systemd can check if the process is still running. If that is not the case, systemd can either quietly restart the process, send an alert, or do whatever you want to happen.
As Pi3D needs a different start command depending if you are using the Raspberry Pi 4 or the older models 2 and 3, I will show you both ways.
Instructions for the Raspberry Pi 2 and 3
From within the command line in the terminal create a new systemd service by typing:
sudo nano /lib/systemd/system/pi3d.service
Paste this text into the window:
[Unit] Description=Pi3D Service After=multi-user.target [Service] Type=idle User=pi ExecStart=/usr/bin/python3 /home/pi/pi3d_demos/PictureFrame.py Restart=always RestartSec=30 [Install] WantedBy=multi-user.target
Save the file and change the permissions to make it readable by all.
sudo chmod 644 /lib/systemd/system/pi3d.service
As the last step, you need to tell the system that you have added this file and want to start the service every time your Raspberry Pi boots up.
sudo systemctl daemon-reload sudo systemctl enable pi3d.service
Reboot your Pi, and you are all set!
Instructions for the Raspberry Pi 4
As the Pi 4 has a more powerful graphics engine, the command for launching Pi3D is different.
With regards to using systemd this means that the third paragraph in the pi3d.service file needs to be modified.
So instead of entering
User=pi ExecStart=/usr/bin/python3 /home/pi/pi3d_demos/PictureFrame.py
change these two lines to
User=root ExecStart=xinit /usr/bin/python3 /home/pi/pi3d_demos/PictureFrame.py :0 -- -s off -dpms -s noblank
Everything else stays the same.
You may have stumbled upon this article after having searched for hours for a solution to your Pi3D image viewer autostart problem. I hope systemd will work for you as reliably as it has for many Raspberry Pi users!
As always, leave me a message with any questions you may have.