I have script in python that need to be run with "sudo" keyword. Everything because I'm accessing "/dev/mem". Now I need to start this python script on RPi startup. So I create simple service that launch this script it looks like :

[Unit]
Description=sendService
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u send.py
WorkingDirectory=/home/pi/pywork
User=root

[Install]
WantedBy=multi-user.target

it is inside /etc/systemd/system directory. I observe that this script is trying to work properly but it gives me error about my python script (that access to /dev/mem is denied). Same problem I have when I use to run this script using "python3 send.py" commend but when I use "sudo python3 send.py" it works like charm. Does it means that this service run mine program without sudo privileges even if user is set to root ?

Edit:

systemctl status gives

● serviceSend.service - sendService
   Loaded: loaded (/etc/systemd/system/serviceSend.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Fri 2018-08-10 10:36:43 UTC; 3min 35s ago
  Process: 1306 ExecStart=/usr/bin/python3 -u send.py (code=exited, status=0/SUCCESS)
 Main PID: 1306 (code=exited, status=0/SUCCESS)

Aug 10 10:36:42 raspberrypi systemd[1]: Started sendService.
Aug 10 10:36:43 raspberrypi sudo[1320]:     root : TTY=unknown ; PWD=/home/pi/pywork ; USER=root ; COMMAND=/bin/systemctl stop serial-getty@ttyAMA0.service
Aug 10 10:36:43 raspberrypi sudo[1320]: pam_unix(sudo:session): session opened for user root by (uid=0)
Aug 10 10:36:43 raspberrypi sudo[1320]: pam_unix(sudo:session): session closed for user root

Your unit looks fairly good. From the status I don't understand why there are lines from [sudo]

raspberrypi sudo[1320]:     root : TTY=unknown ; PWD=/home/pi/pywork ; USER=root ; COMMAND=/bin/systemctl stop serial-getty@ttyAMA0.service
raspberrypi sudo[1320]: pam_unix(sudo:session): session opened for user root by (uid=0)
raspberrypi sudo[1320]: pam_unix(sudo:session): session closed for user root

It seems there is something opening a sudo session (pam_unix(sudo:session)) and tries to write to a tty (standard output?). But on a starting service there is no tty (TTY=unknown) so you cannot output to it. Maybe this is from the option -u (unbuffered binary stdout and stderr) invoked with python3?

Also your "service" does not continue to run like a service in the background. It ended inactive (dead) regular with (code=exited, status=0/SUCCESS). So I assume it is a oneshot.

Services are executed with root rights by default so I would omit User=root.

So for the first attempt I would suggest to try this unit:

[Unit]
Description=sendService
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/python3 send.py
WorkingDirectory=/home/pi/pywork

[Install]
WantedBy=multi-user.target
  • Now I get photo-screen seems to be better but still script start and exited – kyluace Aug 10 at 12:23
  • What does your script do? Seems it needs some environment that doesn't exist on startup. – Ingo Aug 10 at 12:30
  • this python script works quite like a server, but like I said before when I use command line to start it "sudo python3 send.py" it works like charm so there is no problem with script. – kyluace Aug 10 at 12:33
  • Like a server? Should it always run in the background? With sudo on the command line you have the user environment that isn't present on startup. Look at man systemd.exec what you can do to setup the needed environment. And is it possible that you show us the script? You can edit your question for this. – Ingo Aug 10 at 12:51

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.