Skip to content

Instantly share code, notes, and snippets.

@Lulzx
Created November 8, 2018 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lulzx/5c35274591b79e819ca9d7d4b1eacaf2 to your computer and use it in GitHub Desktop.
Save Lulzx/5c35274591b79e819ca9d7d4b1eacaf2 to your computer and use it in GitHub Desktop.
kill me
import os
import sys
import signal
import subprocess
currentPID = os.getpid()
if 'pid' not in os.listdir():
with open('pid', mode='w') as f:
print(str(currentPID), file=f)
else:
with open('pid', mode='r') as f:
try:
os.kill(int(f.read()), signal.SIGTERM)
print("Terminating previous instance of " +
os.path.realpath(__file__))
except ProcessLookupError:
subprocess.run(['rm', 'pid'])
with open('pid', mode='w') as f:
print(str(currentPID), file=f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment