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.

        # Do a bunch of GPIO stuff            

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?


When power is applied to the Raspberry Pi all GPIO are set as inputs.

That makes anything done by the cleanup() method irrelevant.

  • Thanks! I couldn't find that but it is good to know. Apr 18 '18 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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