I've been trying to implement a custom procedure triggered by a button connected to my raspberry pi. To do that I've tried to launch a python script on boot that always listens to the button trigger.
That's the python script I'd like to run (just makes two system calls on button trigger).
#!/usr/bin/env python
#reset.py
import pigpio
import subprocess
import time
BUTTON = 16
pi = pigpio.pi('localhost')
pi.set_mode(BUTTON, pigpio.INPUT)
pi.set_pull_up_down(BUTTON, pigpio.PUD_UP)
if(pi.wait_for_edge(BUTTON, edge = pigpio.FALLING_EDGE, wait_timeout = 5)):
subprocess.call(["sudo", "backup_boot"])
subprocess.Popen(["sudo", "restore_boot"], cwd="/home/pi/overlay-factory-reset/src/sbin/")
I've moved the script into /usr/local/bin with
sudo mv reset.py /usr/local/bin/
sudo chmod +x /usr/local/bin/reset.py
I've tested it with python reset.py and everything works fine. I've then created a shell script that handles the scrip on system boot
#!/bin/sh
#listen-for-reset.sh
BEGIN INIT INFO
Provides: reset.py
Required-Start: $remote_fs $syslog
Required-Stop: $remote_fs $syslog
Default-Start: 2 3 4 5
Default-Stop: 0 1 6
END INIT INFO
If you want a command to always run, put it here
echo PROVA
Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting reset.py"
/usr/local/bin/reset.py &
;;
stop)
echo "Stopping reset.py"
pkill -f /usr/local/bin/reset.py
;;
*)
echo "Usage: /etc/init.d/listen-for-reset.sh {start|stop}"
exit 1
;;
esac
exit 0
that I then moved into /etc/init.d and made it executable
sudo mv listen-for-reset.sh /etc/init.d/
sudo chmod +x /etc/init.d/listen-for-reset.sh
Then I've registered the script to start on boot
sudo update-rc.d listen-for-reset.sh defaults
Now, if I run
sudo /etc/init.d/listen-for-reset.sh start
the script is called correctly, and performs its job. If I reboot the pi, the script is not launched at boot and I have to start it manually with the command above.
I've tried other solutions like editing rc.local (I also verified the creation of the link inside the rc folder and it's there) and using crontab but none of them worked. It seems like there's a problem in services during the pi boot
I've no clue at all how to resolve the problem.
EDIT I've also tried with systemd but didn't work either. I've tried all the methods here listed
I also verified the creation of the link inside the rc folder
... you mean the rc?.d folders, and the links start withS
notK
, correct? Have you checked the system logs for errors? perhaps$remote_fs $syslog
isn't the correct "required start" for your program ... or it could be that running as root is the problem ... doessudo python reset.py
also work fine? – Jaromanda X Feb 15 '19 at 12:11/etc/rc2.d
and yes the link starts with S,S02listen-for-reset.sh
.sudo python reset.py
works fine, and everything works fine by starting manually withsudo /etc/init.d/listen-for-reset.sh start
but it doesn't start on boot. About the$remote_fs
etc I've taken the parameter from this website: https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi It sets up a button for shutdown so I guess it will run at boot as well – Alex Feb 15 '19 at 12:14