From ae84d09e8e6fb9dcc49bbb6a3573f2b80e629074 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 22 Dec 2020 08:57:15 -0600 Subject: [PATCH] * detect stale run file fixes #27 --- src/onionrcommands/daemonlaunch/__init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/onionrcommands/daemonlaunch/__init__.py b/src/onionrcommands/daemonlaunch/__init__.py index 9a980725..67d454ea 100755 --- a/src/onionrcommands/daemonlaunch/__init__.py +++ b/src/onionrcommands/daemonlaunch/__init__.py @@ -13,6 +13,7 @@ import stem import toomanyobjs import filenuke from deadsimplekv import DeadSimpleKV +import psutil import config import onionrstatistics @@ -257,6 +258,20 @@ def start(override: bool = False): pass else: return + with open(filepaths.lock_file, 'r') as lock_file: + try: + proc = psutil.Process(int(lock_file.read())).name() + except psutil.NoSuchProcess: + proc = "" + if not proc.startswith("python"): + logger.info( + f"Detected stale run file, deleting {filepaths.lock_file}", terminal=True) + try: + os.remove(filepaths.lock_file) + except FileNotFoundError: + pass + start(override=True) + return logger.fatal('Cannot start. Daemon is already running,' + ' or it did not exit cleanly.\n' + ' (if you are sure that there is not a daemon running,' @@ -265,7 +280,7 @@ def start(override: bool = False): else: if not onionrvalues.DEVELOPMENT_MODE: lock_file = open(filepaths.lock_file, 'w') - lock_file.write('delete at your own risk') + lock_file.write(str(os.getpid())) lock_file.close() # Start Onionr daemon