aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerg_samowzbudnik <uinarf@autistici.org>2021-06-12 10:14:10 +0200
committererg_samowzbudnik <uinarf@autistici.org>2021-06-12 10:14:10 +0200
commit65d2b5badda284dd4e5a4f97846bf53cf866f2e5 (patch)
tree85b4deb859cb5f877dba2639d320b822eb64c65f
parenta3477b6c374145d9f1cb55b88b1a0a2db9ea46c7 (diff)
downloadRPGH-65d2b5badda284dd4e5a4f97846bf53cf866f2e5.tar.gz
RPGH-65d2b5badda284dd4e5a4f97846bf53cf866f2e5.tar.bz2
RPGH-65d2b5badda284dd4e5a4f97846bf53cf866f2e5.zip
adding changes for interoperability with openrc
-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")