aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--monitor_unified.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/monitor_unified.py b/monitor_unified.py
index e68c1f4..0ce8133 100644
--- a/monitor_unified.py
+++ b/monitor_unified.py
@@ -11,6 +11,8 @@ __email__ = "uinarf@autistici.org"
Program intended to be run as daemon reading from sensors and
writing to database.
"""
+import signal
+import psutil #optional
import sys, os, glob, re
from time import strftime, localtime, sleep
@@ -490,6 +492,42 @@ class Rain(Sensor):
# GPIO.add_event_detect(self.pin, GPIO.FALLING, callback=self.read(),
# bouncetime=300)
+def clean_exit(sig,frame):
+ #do some other cleanup if you want
+ os.remove('/var/run/user/[your uid]/RPGH_starter.pid')
+ os.exit(0)
+
+def start():
+ signal.signal(signal.SIGTERM, clean_exit)#from start-stop-daemon
+ signal.signal(signal.SIGINT, clean_exit)#from ctrl-c
+ with open('/var/run/user/[your uid]/RPGH_starter.pid', 'x') as pid_file:
+ pid_file.write(str(os.getpid()))
+ do_main_loop()
+
+def main():
+ try: action=sys.argv[1]
+ except IndexError: action='stop'
+ if action=='start':
+ start()
+ elif action=='start_fork':
+ if os.fork():sys.exit(0)
+ start()
+ elif action=='stop'
+ with open('/var/run/user/[your uid]/RPGH_starter.pid', r') as pid_file:
+ pid=int(pid_file.read())
+ os.kill(pid,signal.SIGTERM)
+
+ #optional clean up
+ try:
+ child=psutil.Process(pid)
+ except FileNotFoundError:#psutil didn't find the pid dir in /proc
+ sys.exit()
+ try:
+ child.wait(timeout=10)
+ except psutil.TimeoutExpired:#we didn't exit/clean up in time
+ child.kill()
+ os.remove('/var/run/user/[your uid]/RPGH_starter.pid')
+
if __name__ == '__main__':
fq = cfg.getint('hardware_settings', 'read_frequency')
print("Instantiating gpio connection")