Skip to content

Instantly share code, notes, and snippets.

@captainsafia
Created August 30, 2012 19:46
Show Gist options
  • Save captainsafia/3539048 to your computer and use it in GitHub Desktop.
Save captainsafia/3539048 to your computer and use it in GitHub Desktop.
Python Application to Kill a Process
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