From fdbeed40f08daf848ac7ca16702e73b1a95d1db6 Mon Sep 17 00:00:00 2001 From: Krateng Date: Mon, 2 Sep 2019 01:22:17 +0200 Subject: [PATCH] Added supervisor to restart on crash, very experimental --- maloja | 15 ++++++++++++++- supervisor.py | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 supervisor.py diff --git a/maloja b/maloja index 5fc9eab..cadd7eb 100755 --- a/maloja +++ b/maloja @@ -183,12 +183,21 @@ def getInstance(): except: return None +def getInstanceSupervisor(): + try: + output = subprocess.check_output(["pidof","maloja_supervisor"]) + pid = int(output) + return pid + except: + return None + def start(): if install(): if gotodir(): setup() p = subprocess.Popen(["python3","server.py"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) + p = subprocess.Popen(["python3","supervisor.py"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) print(green("Maloja started!") + " PID: " + str(p.pid)) from doreah import settings @@ -221,8 +230,12 @@ def restart(): return wasrunning def stop(): + pid_sv = getInstanceSupervisor() + if pid_sv is not None: + os.kill(pid,signal.SIGTERM) + pid = getInstance() - if pid == None: + if pid is None: print("Server is not running") return False else: diff --git a/supervisor.py b/supervisor.py new file mode 100644 index 0000000..706ed13 --- /dev/null +++ b/supervisor.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +import subprocess +import time +import setproctitle +import signal + + +setproctitle.setproctitle("maloja_supervisor") + + +while True: + time.sleep(60) + + try: + output = subprocess.check_output(["pidof","Maloja"]) + pid = int(output) + except: + print("Maloja not running, restarting...") + p = subprocess.Popen(["python3","server.py"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)