Created
May 27, 2013 13:48
-
-
Save dreikanter/5657159 to your computer and use it in GitHub Desktop.
How to kill a process using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PID_FILE = 'pid.txt' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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