I have a python script that runs in the background on startup. The starting method is a entry in a run.sh file which is called with /etc/rc.local. The exact entry would be "sudo python /home/pi/run/main.py &". The system is a raspberry pi with wheezy.
The script is running, no problem so far. If a shutdown command is send to the system (via console "sudo shutdown -h now") I need further the script to not abort right away but to execute some code first. Thats what I got so far:
#!/usr/bin/env python
import atexit
@atexit.register
def byebye():
c = "End"
datei = open("/home/pi/logfile",'a+b')
datei.write(c + "\n")
datei.close()
def main():
while True:
...do anything...
main()
Right now it seems to just exit the main loop on shutdown. Do I need to use a different way to shutdown the system so the signal is transmitted to my script or did I maybe not get the usage of the "@atexit" method? Any ideas?
Thanks