I want a simple python script to run in background, therefore I configured daemon
[Unit] Description=Test service After=multi-user.target [Service] Type=simple ExecStart=/usr/bin/python /home/pi/daemon.py Restart=on-abort [Install] WantedBy=multi-user.target
My executing script
daemon.py toogles a LED controlled by a button.
#!/usr/bin/python import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # Configure button pin GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Configure LED pin GPIO.setup(18, GPIO.OUT) def button1(channel): print "Toogle LED!" GPIO.output(18, not GPIO.input(18)) GPIO.add_event_detect(14, GPIO.FALLING, callback=button1, bouncetime=300) try: while True: pass finally: print "cleanup() GPIOs" GPIO.cleanup()
Now, I can start my script as a service with
systemctl start test.service and stop it the same way. But for a clean script, I want to execute
GPIO.cleanup() before the script is closed by
systemctl stop test.service. How can I achieve that? The
finally doesn't seem to work in this case...
Secondly, all the print messages like "Toogle LED!" cannot be seen when I call
systemctl status test.service. Is the output written to another file somewhere else?
Thank you for your help in advance!