From 681d990f906578e7fa5dca9ce1e7c6f202e8ad6e Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 21 Nov 2013 05:52:55 +0100 Subject: [PATCH 1/4] Added pid option --- overviewer.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/overviewer.py b/overviewer.py index 71bdb14..2f85384 100755 --- a/overviewer.py +++ b/overviewer.py @@ -66,6 +66,7 @@ def main(): parser.add_option("-p", "--processes", dest="procs", action="store", type="int", help="The number of local worker processes to spawn. Defaults to the number of CPU cores your computer has") + parser.add_option("--pid", dest="pid", action="store", help="Specify the pid file to use.") # Options that only apply to the config-less render usage parser.add_option("--rendermodes", dest="rendermodes", action="store", help="If you're not using a config file, specify which rendermodes to render with this option. This is a comma-separated list.") @@ -138,6 +139,9 @@ def main(): print("(build info not found)") return 0 + if options.pid: + 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 # operate on a custom texture path. we do terrain checking with a custom texture # pack later on, after we've parsed the config file @@ -493,6 +497,8 @@ dir but you forgot to put quotes around the directory, since it contains spaces. logging.debug("Final cache stats:") for c in caches: logging.debug("\t%s: %s hits, %s misses", c.__class__.__name__, c.hits, c.misses) + if options.pid: + os.remove(options.pid) return 0 From f71ede45f8faae5882f0328c578bd51819b0049f Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 26 Nov 2013 20:03:57 +0100 Subject: [PATCH 2/4] Exit if pid file already exists --- overviewer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/overviewer.py b/overviewer.py index 2f85384..a1cb800 100755 --- a/overviewer.py +++ b/overviewer.py @@ -140,6 +140,9 @@ def main(): return 0 if options.pid: + if os.path.exists(options.pid): + print("Already running (pid exists) - exiting..") + return 0 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 From c56e12d94d905b27872e7dcff3ad078b5ecf40ab Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 26 Nov 2013 20:14:06 +0100 Subject: [PATCH 3/4] Fix indentation --- overviewer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/overviewer.py b/overviewer.py index a1cb800..74d4394 100755 --- a/overviewer.py +++ b/overviewer.py @@ -140,9 +140,9 @@ def main(): return 0 if options.pid: - if os.path.exists(options.pid): - print("Already running (pid exists) - exiting..") - return 0 + if os.path.exists(options.pid): + print("Already running (pid exists) - exiting..") + return 0 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 From 5bc3f13b4ff5a231737a9587626c42b0014b080d Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 26 Nov 2013 20:36:53 +0100 Subject: [PATCH 4/4] 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