Skip to content

Instantly share code, notes, and snippets.

@masnun
Created May 1, 2014 17:16
Show Gist options
  • Save masnun/7a3e9df78cc27af73ed7 to your computer and use it in GitHub Desktop.
Save masnun/7a3e9df78cc27af73ed7 to your computer and use it in GitHub Desktop.
from subprocess import Popen, PIPE, call
from sys import argv, exit
from time import sleep
from os import getpid
def get_process_list(app):
ps_aux = Popen(["ps", "aux"], stdout=PIPE)
grep = Popen(["grep", app], stdin=ps_aux.stdout, stdout=PIPE)
awk = Popen(["awk", "{print $2}"], stdin=grep.stdout, stdout=PIPE)
ps_aux.stdout.close()
grep.stdout.close()
return awk.communicate()[0].strip("\n").split("\n")
def kill_process_list(process_list):
devnull = open("/dev/null", "w")
for x in process_list:
if str(x) != str(getpid()):
kill = Popen(["kill", x], stdout=devnull, stderr=devnull)
def timed_kill():
try:
app, minutes = argv[1:3]
sleep(int(float(minutes) * 60))
kill_process_list(get_process_list(app))
except Exception, ex:
print ex.message
print " Invalid argument\n Usage : tkill <app name> <time in minutes>"
if __name__ == "__main__":
timed_kill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment