I am new to UNIX and PYTHON, like soe many others brought to it by the mission of RPi. I am experimenting with running a python script as a daemon on boot using start-stop-daemon. I have used google, various other sites, etc to try and learn more about how all of this works so that I can understand my problem, but I am stuck and am hoping that someone can point the way. Being new, I may not know enough to recognize the solution when I see it, or to search for it correctly.
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).
Code: Select all
import RPi.GPIO as GPIO
from time import sleep
print "loop ending"
print "cleaning up"
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?