Problem starting Python-script via etc/rc.local

Hey guys,

I am desperate sitting on a small project for far too long.

I managed to write a little script via Python that allows me to choose a webradio station with a keypad. The script works just fine when used over SSH. Now i want to add that script to autostart.

I edited rc.local to the following:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

sudo python /home/volumio/keypad-rpi/example.py &

exit 0

The problem is, that nothing happens on the next startup. I can’t even say if the script is executed.

Maybe there is a problem with the password for the volumio user, which I every time get asked for when I edit or execute the script.

Does anybody know how to solve this?

[Edit]The script somehow started when I returned from sleep mode in the Volumio WebUI. The Script was not executed via SSH. Either autostart works from sleep mode or something spooky happened. :unamused:

Thanks!

My setting: Raspberry Pi2 B, Volumio 2.041

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

[code][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[/code]

Make sure you have the shebang line at the beginning of your script, something like

#!/usr/bin/python

Save it, then enable it

systemctl enable myscript.service

then start it with

systemctl status myscript.service

and finally, check if its running with

systemctl status myscript.service

Hope that helps, let me know

Thank you for your fast respond. Your instruction is great but it seems it is not working. When I am checking the status after enabling and restart the system it says:

volumio@volumio:~$ systemctl status myscript.service -l
● myscript.service - Volumio Backend Module
   Loaded: loaded (/lib/systemd/system/myscript.service; enabled)
   Active: failed (Result: start-limit) since Sat 2017-02-25 18:03:12 UTC; 2min44s ago
  Process: 627 ExecStart=/home/volumio/keypad-rpi/example.py (code=exited, status=203/EXEC)
 Main PID: 627 (code=exited, status=203/EXEC)

Feb 25 18:03:11 volumio systemd[1]: Unit myscript.service entered failed state.
Feb 25 18:03:12 volumio systemd[1]: myscript.service holdoff time over, scheduling restart.
Feb 25 18:03:12 volumio systemd[1]: Stopping Volumio Backend Module...
Feb 25 18:03:12 volumio systemd[1]: Starting Volumio Backend Module...
Feb 25 18:03:12 volumio systemd[1]: myscript.service start request repeated too quickly, refusing to start.
Feb 25 18:03:12 volumio systemd[1]: Failed to start Volumio Backend Module.
Feb 25 18:03:12 volumio systemd[1]: Unit myscript.service entered failed state.

I don’t know if you can tell me where the problem is. I am not sure how to enter the shebang line exactly. I simple put the exact phrase ‘#!/usr/bin/python’ in front of my script. Is that correct?

I am also not sure if I got the content of the myscript.service file right. I just “filled” it this way:

[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

Do not wonder about the script name by the way. Its really called “example”, so there should not be the problem.

I hope there is a solution and I don’t bother anyone. I am new to this;)

Huh? I have “irexec” and “thd” (triggerhappy) starting in rc.local, and they seem to work (since rc.local is run as root, I don’t need sudo there, but I need full path for configuration files).

Little Update of my recent progress:

It works just fine! It seems that I had the shebang line wrong. It works exactly the way michelangelo described it. Thanks for that!

I have a new problem:

When I try to start another script on startup the way michelangelo described, it does not work.

The new script should allow me to shutdown my Raspi with the push of a button and a LED confirming that. This is how my script looks like:

#!/usr/bin/python
import RPi.GPIO as GPIO
import os
import time

# use the pin number as on the raspi board

GPIO.setmode(GPIO.BOARD)

# set pin 7 as output and HIGH, pin 5 is input

GPIO.setup(7, GPIO.OUT)
GPIO.output(7, True)
GPIO.setup(5, GPIO.IN)

# start the loop for every .5 seconds, waiting for LOW on pin 5
# then 2 short flashes with led to confirm and shutdown to sleep mode

while True:
        if not (GPIO.input(5)):
                GPIO.output(7, False)
                time.sleep(.1)
                GPIO.output(7, True)
                time.sleep(.1)
                GPIO.output(7, False)
                time.sleep(.1)
                GPIO.output(7, True)
                os.system("sudo shutdown -h now")
        time.sleep(.5)

It works fine when executed via SSH but when I try to auto start it this way…


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

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

[Install]
WantedBy=multi-user.target

…enable and start it, i get this status:

volumio@volumio:~/keypad-rpi$ systemctl status myscript.service -l
● myscript.service - Volumio Backend Module
   Loaded: loaded (/lib/systemd/system/myscript.service; enabled)
   Active: failed (Result: start-limit) since Mon 2017-03-06 17:01:32 UTC; 3s ago
  Process: 1336 ExecStart=/home/volumio/keypad-rpi/shutdown.py (code=exited, status=1/FAILURE)
 Main PID: 1336 (code=exited, status=1/FAILURE)

Mar 06 17:01:32 volumio systemd[1]: Unit myscript.service entered failed state.
Mar 06 17:01:32 volumio systemd[1]: myscript.service holdoff time over, scheduling restart.
Mar 06 17:01:32 volumio systemd[1]: Stopping Volumio Backend Module...
Mar 06 17:01:32 volumio systemd[1]: Starting Volumio Backend Module...
Mar 06 17:01:32 volumio systemd[1]: myscript.service start request repeated too quickly, refusing to start.
Mar 06 17:01:32 volumio systemd[1]: Failed to start Volumio Backend Module.
Mar 06 17:01:32 volumio systemd[1]: Unit myscript.service entered failed state.

The shutdown script is in the same directory like my “example” script from before. So that should not be the problem here.

Maybe there is someone who can help me with this issue?!

Thanks guys!

You may want to look at GPIO Button Volumio plugin, which simply allows you set such GPIO shutdown action (and many others) with no programing (except led blinking, but could be added maybe).
This plugin is accessible from Volumio Plugin UI, or you may want to use/modify source here (config-rewrite branch is recommanded).

I have been using a different approach for running Python using “tmux”.
(tmux can be installed in SSH by typing “sudo apt install tmux”.)
(To learn more about the joys of tmux see https://danielmiessler.com/study/tmux/)

Note I am NOT attemptingg to run the Python direct from rc.local. I dont remember if that is because the method I use was recomended or if running direct in rc.local has a problen of some sort…

Currently I am using it to run a modified version of the Python3 prog supplied by Pimorini that controls the RPi4 cooling fan at a slower speed to make it queter. (see [uhttps://github.com/grayerbeard/fanshim-python-pwm/tree/volumio[/url]) where I explain some of the other issue getting Python Code that uses GPIO to work and I have done all the adjustments to change from user “pi” to user “volumio”.

On seeing the comments above I thought I had better check if it ran on the current version of Volumio.
I found it was running fine on both the older 2.713 I had been using and the latest 2.714.

This is my rc.local file

[code]#!/bin/sh -e

rc.local

This script is executed at the end of each multiuser runlevel.

Make sure that the script will “exit 0” on success or any other

value on error.

In order to enable or disable this script just change the execution

bits.

By default this script does nothing.

sudo -u volumio. bash ./home/volumio/fanshim/tmux_start sh &
exit 0
[/code]

and this is the script that starts the program (its the "./home/volumio/fanshim/tmux_start sh " which must be set as “executable”.

#!/bin/bash cd /home/volumio/fanshim echo looking to kill any existing fanshim session tmux kill-session -t fanshim echo now new tmux fanshim session tmux new-session -d -s fanshim 'python3 examples/automatic.py --off-threshold 54 --on-threshold 56 --verbose'