0

For context, I am using Raspberry Pi model 3B+. Currently I am trying to run a python script at the Pi's boot up. But I am struggling with getting the program to launch properly. When I use the following service file, it does launch but it relaunches every 10 seconds causing the program to be launched infinitely many times. I just want the program to launch once.

[Unit]
Description=Start Bling

[Service]
User=pi
Environment=DISPLAY=:0
WorkingDirectory=/home/pi/facial_recognition
Environment=XAUTHORITY=/home/pi/.Xauthority
Environment="prog_path"=/home/pi/facial_recognition
ExecStart=/usr/bin/python /home/pi/facial_recognition/run_on_start.py

Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity

[Install]
WantedBy=graphical.target

But if I remove this part:

Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity

or change it to Restart=on-failure, nothing happens when the Raspberry Pi boots up.

The program does not necessarily need to run in Desktop auto-login so if there is a possible fix in the console version that is fine as well. I just have it this way currently, so it is easier to check if the program is working as intended.

I have also tried using crontab, but then nothing happened on reboot. Not with and not without the '&' at the end.

1 Answer 1

1

Here's what I think is happening:

Your machine boots up, the display server starts, and your service is activated. However pi is not logged in yet and so it fails to start the first time. Your Restart=always and Restart=10s ensure that it tries to launch every 10s, and it does! But somehow your main process is exiting and the Restart= is triggered continuously.

Here's an idea for solving it:

Move the service to the user bus. This also means you don't need to define DISPLAY, XAUTHORITY, or User= as those are implicit. Then change the [Install] section to WantedBy=graphical-session.target. Add PartOf=graphical-session.target to stop the service when the user logs out of the session. graphical-session.target is a target that is only available on the user-bus and is only raised when the user is logged into a graphical session. The Restart/RestartSec/KillMode/TimeoutSec only needs to be defined if you really need it for something (not just to get your program to start).

This will ensure your service doesn't start before the user is logged into the graphical session (which I think is what prevented it from working before).

To do that, move your service file from /etc/systemd/system/ to /home/pi/.config/systemd/user/bling.service and have it look like this:

[Unit]
Description=Bling
PartOf=graphical-session.target

[Service]
WorkingDirectory=/home/pi/facial_recognition
Environment="prog_path"=/home/pi/facial_recognition
ExecStart=/usr/bin/python /home/pi/facial_recognition/run_on_start.py

[Install]
WantedBy=graphical-session.target

Then enable it with systemctl --user enable bling.service

4
  • This sadly still does not run the program when rebooting or starting up, nothing happens. Maybe it has something to do with the fact that my systemd is not located in /home/pi/.config/, I can't find it there. For me it is located in /lib/systemd/ so I moved the service file to /lib/systemd/user. Could that be the cause of the program not launching?
    – Luc
    Commented Oct 27, 2021 at 12:14
  • Yes. /lib/systemd/system is where packages put services. If you edit or create your own system service, it goes in /etc/systemd/system. If you create a specific user service, put it in ~/.config/systemd/user. If that doesn't doesn't exist, you may create it. Also... test with systemctl --user status *.service and then if it works, enable and reboot. Note that you shouldn't use sudo when working with the user bus
    – Stewart
    Commented Oct 27, 2021 at 12:19
  • Tried some more things, in the different directories but no result. If I run the --user status *.service I get Unit bling.service could not be found. as a result. So I guess it is not recognizing it or not running it for some reason.
    – Luc
    Commented Oct 27, 2021 at 12:42
  • Double check that you are logged in as user pi and not using sudo. It should find /home/pi/.config/systemd/user/bling.service
    – Stewart
    Commented Oct 27, 2021 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .