Page 1 of 1

Unable to write a .service file to systemd for auto-booting python programs

Posted: Tue Oct 01, 2019 6:45 pm
by Nathan.Rivard
So I have written a service file that should automatically start up the program that I have already created and would just like to begin on boot whenever the unit shuts off/fails and has to reboot
The service file is as follows:

Code: Select all

[Unit]
Description=YourDescription

[Service]
ExecStart=sudo /usr/bin/python /home/pi/Desktop/FolderOnDesktop/Program.py

[Install]
WantedBy=multi-user.target
My issues comes about with my denied access to writing to the /home/pi/systemd/system folder. I am told upon either writing from the command prompt or just placing my service file within the folder that I am denied access.
My two questions:
Would the code that I have written for the service file start my program on boot and allow it to write to a .csv file that the program already does?
How do I gain access to the systemd folder?

Re: Unable to write a .service file to systemd for auto-booting python programs

Posted: Tue Oct 01, 2019 8:34 pm
by knute
The .service files need to go into the /etc/systemd/system directory. That directory is owned by root. The .service file needs to be owned by root. So you need to use sudo on all the command to read or write a file to that directory.

Also, the systemd services are run as root unless you specifically say they aren't. So you don't need the sudo on the ExecStart line.

Re: Unable to write a .service file to systemd for auto-booting python programs

Posted: Wed Oct 02, 2019 1:32 pm
by Nathan.Rivard
I know this is a dumb question, but how do I make the file owned by the root?

Re: Unable to write a .service file to systemd for auto-booting python programs

Posted: Wed Oct 02, 2019 2:28 pm
by DougieLawson
Nathan.Rivard wrote:
Wed Oct 02, 2019 1:32 pm
I know this is a dumb question, but how do I make the file owned by the root?
sudo chown root.root /file/to/be/owned/byroot

Re: Unable to write a .service file to systemd for auto-booting python programs

Posted: Wed Oct 02, 2019 2:41 pm
by Douglas6
Since you are creating the file, simply use sudo to create it.

Code: Select all

sudo nano /etc/systemd/system/my.service
Then you'll need to enable it:

Code: Select all

sudo systemctl enable my.service
and reboot.

No you can begin debugging the remaining issues, like file paths, environment, dependencies, etc.

Re: Unable to write a .service file to systemd for auto-booting python programs

Posted: Wed Oct 02, 2019 2:58 pm
by scruss
You can run services as a regular user too. They run from the ~/.config/systemd/user folder. Here's one way of doing it (self link): Creating a systemd user service on your Raspberry Pi

I'm not sure how well sudo plays with systemd. Do you need it for your script? If you're running a system (root) systemd service it will either be ignored or fail.

Re: Unable to write a .service file to systemd for auto-booting python programs

Posted: Wed Oct 02, 2019 3:17 pm
by DougieLawson

Code: Select all

ExecStart=sudo 
You do NOT need that. All systemd services run with root permissions unless you add user/group

Code: Select all

[Service]
ExecStart=/usr/local/bin/myServicerunningasPi
Restart=always
User=pi
Group=pi
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myNonPrivilegedService