I see that someone - the OP? - marked this as "[SOLVED]", but at best what I see here is a workaround. Does anyone know how to actually solve it? I have the same problem.
I have a headless Pi running some software for a MIDI project of mine. I attached a button (to GPIO pin 7) so that I can issue a shutdown command so as to take the system down nicely. When running for real, the shutdown command kills the program, fine, but when I run in test mode (see below) I *have* to <ctrl>C the program, I can't just exit nicely. (Which is OK, but bugs me.)
Anyone have a real solution?
Code: Select all
#!/usr/bin/env python3
# Wait for GPIO pin 7 to get grounded, then shut the machine down.
# args: [--test]
from datetime import datetime
from gpiozero import Button
import os
import signal
import sys
import time
testing_ = False
if len(sys.argv) == 2 and sys.argv[1] == '--test':
testing_ = True
print('Setting test mode')
def doShutdown():
msg = '{} POWERING OFF! at {}'.format(sys.argv[0], datetime.now())
if testing_:
msg = '(Testing) ' + msg
os.system('sudo logger -p emerg "{}"'.format(msg))
if testing_:
print('Testing - NOT shutting down')
# why doesn't this work?
# sys.exit(1)
print('You have to hit <ctrl>C, I don''t know why!')
else:
os.system('sudo poweroff')
button = Button(7)
button.when_held = doShutdown
print('Waiting for button hold....')
# This waits, but we can't get out of it - sys.exit() doesn't!
# But it's OK in our real world use, cuz the poweroff command *does* kill everything.
#
signal.pause()