I have a python script (taken from the forums here) that reboots my RPI when a button is pressed.
It works great, no issues, except that it refuses to start automatically (either on boot, login, anything). I have tried various options listed here (rc.local, .bashrc, idit.d tab, systems, crontab) and nothing seems to work.
Is it something as simple as my script actually not being very good, and something in there is stopping it from working?
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os
def button_callback(channel):
print("Button was pushed!")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def Restart(channel):
os.system("sudo shutdown -r now")
GPIO.add_event_detect(16,GPIO.RISING,callback = Restart, bouncetime = 2000)
message = input()
GPIO.cleanup()
-
1May be useful: raspberrypi.stackexchange.com/q/40493/5538goldilocks– goldilocks2020年04月12日 15:34:46 +00:00Commented Apr 12, 2020 at 15:34
-
Thanks @goldilocks , I'll give it a thorough read.lukenm– lukenm2020年04月12日 15:43:50 +00:00Commented Apr 12, 2020 at 15:43
1 Answer 1
At a guess the script is immediately terminated when run in the background because of the message = input()
line. The script's input and output will not be set to the devices you expect (screen and keyboard).
Replace that line with an eternal while loop. Something like the following.
while True:
time.sleep(60)