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.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

  • 1
    How have you tried to auto run it? What does the code look like? Where is it printing text to? Is the process still running after boot?
    – Jacobm001
    Aug 12, 2016 at 21:04
  • Hey all, please keep in mind how comments are to be used...
    – Ghanima
    Aug 12, 2016 at 21:12
  • i have added my code and how i auto ran it Aug 12, 2016 at 21:15
  • .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 say exit 0 - also, when using rc.local you wont need sudo in your script - rc.local is executed as root Aug 12, 2016 at 21:49
  • I'm sure someone else will be kind enough to point out the error in your code. Of course there may well be errors in your wiring as well. As to the code error I suggest a Python tutorial is the best start point.
    – joan
    Aug 13, 2016 at 8:32

1 Answer 1


Your question is incomplete, but from the look of your (apparently) incomplete program it is not running.

For a working solution see https://raspberrypi.stackexchange.com/a/42945/8697

This would also be more robust, and less prone to unwanted shutdown due to spikes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.