Am I on the right track? Goal is to shutdown a pi with a push button. Using the following code it works fine in terminal (with sudo of course):
This works fine in terminal, but I understand that it cannot simply be a cronjob as it requires root privilege for the OS functions.
I looked through the sticky: Running a program at startup.
From my understanding, this would be best handled by systemd. Does this sound like the correct direction to go in? Nothing is "easy" so I'm trying to avoid wasting time on the wrong things.
Code:
from gpiozero import Button # imports button from the Pi GPIO libraryimport timeimport os # imports OS library for Shutdown controlstopButton = Button(27) # defines the button as an object and chooses GPIO 27while True: # infinite loop if stopButton.is_pressed: # Check to see if button is pressed time.sleep(1) # wait for the hold time we want. if stopButton.is_pressed: # check if the user let go of the button os.system("shutdown now -h") # shut down the Pi -h is or -r will reset time.sleep(1) # wait to loop again so we don't use the processor too much.
I looked through the sticky: Running a program at startup.
From my understanding, this would be best handled by systemd. Does this sound like the correct direction to go in? Nothing is "easy" so I'm trying to avoid wasting time on the wrong things.
Statistics: Posted by BlueBarryCat — Mon Aug 05, 2024 4:46 pm — Replies 1 — Views 6