Skip to content

Instantly share code, notes, and snippets.

@palawer
Created October 3, 2019 07:21
Show Gist options
  • Save palawer/d11a58b74e8dedf946c2f29f323ec6cd to your computer and use it in GitHub Desktop.
Save palawer/d11a58b74e8dedf946c2f29f323ec6cd to your computer and use it in GitHub Desktop.
Check if python process is already running
def check_pid(pid):
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
pid = None
try:
pid = int(open("script_name.pid").read())
except:
pass
if pid:
if check_pid(pid):
logging.info("An instance of script_name is already running: PID={}. Bye".format(pid))
sys.exit(0)
pid = os.getpid()
open("script_name.pid","w").write(str(pid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment