Has systemd changed since 2017?

I have tried, many, many times to auto run a python script using systemd.
I found the 2017 Problem starting Payton-script via etc/Rac.local thread and followed the instructions but have had no success. Either I’m making a foolish mistake or the instructions are not relevant today.
If the details in that thread are outdated has anyone an updated version?
If the instructions are correct is there anyone out there who can help. I can give more details if/when necessary.

If you show us what you have done so far, it would be easier to figure out where it might have gone wrong :slight_smile:

Many thanks.

I have a working python script /home/volumio/encoder.py which I want to run at boot.

Following the instructions given in the thread of Feb 2017
’Problem starting Python-script via etc/rc.local’

Rc.local does not work in systemd (what Volumio now uses).
You must go trough systemd itself.
Since your script needs sudo privileges, you can do this (altough this is unsafe…)

Create a file named myscript.service in /lib/systemd/system

It will contain the following

[Unit]
Description = Volumio Backend Module*
Wants=dynamicswap.service*

[Service]
ExecStart=/home/volumio/keypad-rpi/example.py
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myscript
User=root
Group=root

[Install]
WantedBy=multi-user.target

…I created the following file /lib/systemd/system/myscript

#!/usr/bin/python
[Unit]
Description = Volumio Backend Module
Wants=dynamicswap.service

[Service]
ExecStart=/home/volumio/encoder.py
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myscript
User=root
Group=root

[Install]
WantedBy=multi-user.target

(note: I am assuming that the word ‘code’ in square brackets that appears before the [Service] and the [/code] that appears at the end are a throwback to the previous forum formatting. I have tried both with and without them).

I then followed the instructions but get the following error when checking the status of
‘my script.service’

**●** myscript.service - Volumio Backend Module
Loaded: loaded (/lib/systemd/system/myscript.service; enabled)
Active: **failed** (Result: start-limit) since Fri 2021-01-22 15:57:23 UTC; 49s ago
Process: 824 ExecStart=/home/volumio/encoder.py **(code=exited, status=203/EXEC)**
Main PID: 824 (code=exited, status=203/EXEC)
Jan 22 15:57:22 encoder systemd[1]: **myscript.service: main process exited, code=exited, status=203/EXEC**
Jan 22 15:57:22 encoder systemd[1]: **Unit myscript.service entered failed state.**
Jan 22 15:57:23 encoder systemd[1]: myscript.service holdoff time over, scheduling restart.
Jan 22 15:57:23 encoder systemd[1]: Stopping Volumio Backend Module...
Jan 22 15:57:23 encoder systemd[1]: Starting Volumio Backend Module...
Jan 22 15:57:23 encoder systemd[1]: **myscript.service start request repeated too quickly, refusing to start.**
Jan 22 15:57:23 encoder systemd[1]: **Failed to start Volumio Backend Module.**
Jan 22 15:57:23 encoder systemd[1]: **Unit myscript.service entered failed state.**

I have also just noticed that (using terminal on the Mac) my python script now shows up in green, rather than the black that it did when I first saved it. I assume that happened when I ‘systemctl enabled it’
I hope this makes sense. If any further information is needed, please ask.

Many thanks for your interest so far.

Indeed, checking the thread Problem starting Python-script via etc/rc.local - #6 by Schul8er, you seem to have copied the enitre post. For the future, it is simpler to link to the post, and also use the </> tags to embed code that can be copied directly :slight_smile:

For your issue – try something like this as the /lib/systemd/system/encoder.service file.

[Unit]
Description=Encoder Py Module
Wants=volumio.service

[Service]
ExecStart=/home/volumio/encoder.py
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=encoder
User=root
Group=root

[Install]
WantedBy=multi-user.target

But, I would check a few things:
– Does your script launch and do things as expected when launched manually?

volumio@volumio:~$ ./encoder.py

– When should your script launch? You then would have to figure out the Wants= service should be that triggers this service.

Many thanks.

My script did not launch with ./encoder.py (this method was new to me, I had been using python encoder.py which did work.) My script was expecting /usr/bin/python3.4/ I have removed the 3.4 and all is well.

Once again, many thanks.

Ah, when you set

ExecStart=/home/volumio/encoder.py

It basically executes the said file. Which is what the ./encoder.py command does.

In the said file there is a shebang #!/usr/bin/python which tells the shell what interpreter to use the file with. In this case it looks for python, which defaults to python2 on Jessie. If you need python3, then as you have figured, you need to update the shebang accordingly :slight_smile:

PS: Any reason why you don’t use the GPIO-plugin that simplifies such things?

Yes, the plugin worked well but after each restart I needed to re-save the plugin settings to kick it into action. I was also following up an old thread ‘Rotary encoder stops working after reset’ but this way has got there first!

Question. Is there any need to/reason not to add an ampersand to the end of the ExecStart line to make it run in the background. As far as I can see it’s running in the background already, but maybe in a ‘unfriendly’ way?