From 5bc3f13b4ff5a231737a9587626c42b0014b080d Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 26 Nov 2013 20:36:53 +0100 Subject: [PATCH] Exit if already running (pid file) --- overviewer.py | 10 ++++++++-- overviewer_core/util.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/overviewer.py b/overviewer.py index 74d4394..b33acfa 100755 --- a/overviewer.py +++ b/overviewer.py @@ -141,8 +141,14 @@ def main(): if options.pid: if os.path.exists(options.pid): - print("Already running (pid exists) - exiting..") - return 0 + try: + with open(options.pid, 'r') as fpid: + pid = int(fpid.read()) + if util.pid_exists(pid): + print("Already running (pid exists) - exiting..") + return 0 + except IOError, ValueError: + pass with open(options.pid,"w") as f: f.write(str(os.getpid())) # if --check-terrain was specified, but we have NO config file, then we cannot diff --git a/overviewer_core/util.py b/overviewer_core/util.py index d936cba..6b81d8b 100644 --- a/overviewer_core/util.py +++ b/overviewer_core/util.py @@ -24,7 +24,7 @@ import platform from string import hexdigits from subprocess import Popen, PIPE from itertools import cycle, islice, product - +import errno def get_program_path(): if hasattr(sys, "frozen") or imp.is_frozen("__main__"): return os.path.dirname(sys.executable) @@ -391,3 +391,14 @@ try: OrderedDict = collections.OrderedDict except (ImportError, AttributeError): pass + +def pid_exists(pid): # http://stackoverflow.com/a/6940314/1318435 + """Check whether pid exists in the current process table.""" + if pid < 0: + return False + try: + os.kill(pid, 0) + except OSError, e: + return e.errno != errno.ESRCH + else: + return True \ No newline at end of file