Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

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

Tue Oct 01, 2019 6:45 pm

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?

knute
Posts: 477
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

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

Tue Oct 01, 2019 8:34 pm

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.

Nathan.Rivard
Posts: 35
Joined: Tue Sep 10, 2019 3:28 pm

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

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?

User avatar
DougieLawson
Posts: 36327
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

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

Wed Oct 02, 2019 2:28 pm

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
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Douglas6
Posts: 4779
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

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

Wed Oct 02, 2019 2:41 pm

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.

User avatar
scruss
Posts: 2573
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

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

Wed Oct 02, 2019 2:58 pm

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.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
DougieLawson
Posts: 36327
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

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

Wed Oct 02, 2019 3:17 pm

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
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Troubleshooting”