Skip to content

Instantly share code, notes, and snippets.

@dreikanter
Created May 27, 2013 13:48
Show Gist options
  • Save dreikanter/5657159 to your computer and use it in GitHub Desktop.
Save dreikanter/5657159 to your computer and use it in GitHub Desktop.
How to kill a process using Python
PID_FILE = 'pid.txt'
import os
from constants import PID_FILE
if not os.path.exists(PID_FILE):
exit('nothing to kill')
pid = int(open(PID_FILE).read())
print("killing by pid: %d" % pid)
if os.name == 'nt':
os.system("taskkill /pid %d" % pid)
else:
os.kill(pid, signal.SIGKILL)
os.remove(PID_FILE)
import os
from time import sleep, strftime
from constants import PID_FILE
open(PID_FILE, 'w').write(str(os.getpid()))
while True:
sleep(1)
print(strftime("%H:%M:%S"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment