The script uses RPi.GPIO to manage the GPIO inputs and outputs. I have the code set up to run GPIO.cleanup at the end. When I run the script directly in python via the terminal, it runs fine and terminates in a controlled fashion, cleaning up GPIO on its way out. When I run the script as a daemon via start-stop-daemon, it starts ok but it does not gracefully exit when I execute "stop" (never gets to GPIO.cleanup). To illustrate, a simple example (I put the print statements in there to convince myself that it was working the way I thought it should via the terminal).
I have seen references to threads and/or daemons not handling interrupts well, and I suspect that is what I am experiencing here. If start-stop-daemon just kills this little script without letting it exit via "finally:", is there something in RPi.GPIO (or alternatives) that will allow me to clean things up after the daemon is killed? I did try putting GPIO.cleanup() in it's own little script and starting/stopping that as a daemon right after I stop the main script, but it has no effect. I know that it is good practice to cleanup after yourself, so if a daemon does allow you to cleanup from within the script, how do you accomplish it?
Code: Select all
#!usr/bin/python import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setup(27, GPIO.OUT) try: while True: GPIO.output(27,1) sleep(2) GPIO.output(27,0) sleep(2) except KeyboardInterrupt: print "loop ending" finally: print "cleaning up" GPIO.cleanup()