Skip to content

Instantly share code, notes, and snippets.

@EntityReborn
Created May 12, 2011 17:00
Show Gist options
  • Save EntityReborn/968944 to your computer and use it in GitHub Desktop.
Save EntityReborn/968944 to your computer and use it in GitHub Desktop.
import os
import sys
def taskexists(pid):
if sys.platform == "win32":
import ctypes
import win32con
h = ctypes.windll.kernel32.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)
if h:
return True
return False
else:
return os.kill(pid, 0)
def checkInstance(pidfile):
if os.path.isfile(pidfile) and os.access(pidfile, os.F_OK):
with open(pidfile, "r") as f:
pid = f.read()
try:
pid = int(pid)
except:
print "Bad pid: %s" % pid
return False
if taskexists(pid):
print "Already exists!"
return True
os.unlink(pidfile)
return False
if __name__ == "__main__":
pidfile = "socbot.pid"
if not checkInstance(pidfile):
print "FIRST"
pid = str(os.getpid())
with open(pidfile, 'w') as f:
f.write(pid)
# Do some actual work here
#os.unlink(pidfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment