Created
August 30, 2012 19:46
-
-
Save captainsafia/3539048 to your computer and use it in GitHub Desktop.
Python Application to Kill a Process
This file contains hidden or 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 subprocess, signal | |
import os | |
import re | |
a_process = "postgres" | |
def is_running(process): | |
s = subprocess.Popen(["ps", "axw"],stdout=subprocess.PIPE) | |
for x in s.stdout: | |
if re.search(process, x): | |
return True | |
return False | |
if is_running(a_process): | |
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE) | |
out, err = p.communicate() | |
for line in out.splitlines(): | |
if a_process in line: | |
pid = int(line.split(None, 1)[0]) | |
print os.system('kill -9 ' + str(pid)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment