I have a program that turns my pi on and off at a push of a button. The program works and i know that it runs on boot because it prints text and I can see it in the terminal. But what happens is when i press the button, nothing happens. I have tried 2 different ways of auto running the program.
my code
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def shutdown(channel):
os.system("sudo shutdown -h now")
GPIO.add_event_detect(3, GPIO.FALLING, callback = shutdown, bouncetime = 2000)
and i have tried to auto run it by putting "python powerbutton.py" at the bottom of the file /etc/rc.local and .bashrc
.bashrc
is not the place to put the command. When you put it at the bottom of rc.local did you put it ABOVE any lines that sayexit 0
- also, when usingrc.local
you wont need sudo in your script -rc.local
is executed as root – Jaromanda X Aug 12 '16 at 21:49