Exit if already running (pid file)
This commit is contained in:
@@ -141,8 +141,14 @@ def main():
|
|||||||
|
|
||||||
if options.pid:
|
if options.pid:
|
||||||
if os.path.exists(options.pid):
|
if os.path.exists(options.pid):
|
||||||
|
try:
|
||||||
|
with open(options.pid, 'r') as fpid:
|
||||||
|
pid = int(fpid.read())
|
||||||
|
if util.pid_exists(pid):
|
||||||
print("Already running (pid exists) - exiting..")
|
print("Already running (pid exists) - exiting..")
|
||||||
return 0
|
return 0
|
||||||
|
except IOError, ValueError:
|
||||||
|
pass
|
||||||
with open(options.pid,"w") as f:
|
with open(options.pid,"w") as f:
|
||||||
f.write(str(os.getpid()))
|
f.write(str(os.getpid()))
|
||||||
# if --check-terrain was specified, but we have NO config file, then we cannot
|
# if --check-terrain was specified, but we have NO config file, then we cannot
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import platform
|
|||||||
from string import hexdigits
|
from string import hexdigits
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
from itertools import cycle, islice, product
|
from itertools import cycle, islice, product
|
||||||
|
import errno
|
||||||
def get_program_path():
|
def get_program_path():
|
||||||
if hasattr(sys, "frozen") or imp.is_frozen("__main__"):
|
if hasattr(sys, "frozen") or imp.is_frozen("__main__"):
|
||||||
return os.path.dirname(sys.executable)
|
return os.path.dirname(sys.executable)
|
||||||
@@ -391,3 +391,14 @@ try:
|
|||||||
OrderedDict = collections.OrderedDict
|
OrderedDict = collections.OrderedDict
|
||||||
except (ImportError, AttributeError):
|
except (ImportError, AttributeError):
|
||||||
pass
|
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
|
||||||
Reference in New Issue
Block a user