I have been searching for answer to this. I have python script that runs on start up via cron, and lights up some different LEDs based on button pushing using the GPIO pins and runs in the background while my Pi is operating a music player. On system shutdown how do I make sure that GPIO.cleanup() runs? For when I was testing the script, I put the GPIO command in a try ... finally block like this.
try: # Do a bunch of GPIO stuff finally: GPIO.cleanup()
And it would catch a keyboard interrupt for example, but I am not sure if a finally block will run when a shutdown command is sent. Is there a specific exception sent by shutdown that I could catch to make sure the cleanup command runs at shutdown.
Since GPIO.cleanup() only effects pins set in the script, would it be worth it to make a shutdown script that sets all pins to inputs, or am I overthinking all of this?