Last active
December 23, 2015 15:57
-
-
Save simahawk/4195017 to your computer and use it in GitHub Desktop.
Kill any process by name or part of it, and kill it now!
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
#!/usr/bin/python2.7 | |
import sys | |
import os | |
import psutil | |
def find_process(tokill): | |
res = [] | |
for proc in psutil.process_iter(): | |
cmdline = hasattr(proc.cmdline, '__call__') \ | |
and proc.cmdline() or proc.cmdline | |
cmdline = ' '.join(cmdline).lower() | |
if __file__ in cmdline: | |
continue | |
if proc.name == tokill or tokill in cmdline: | |
res.append(proc) | |
return res | |
try: | |
tokill = sys.argv[1] | |
except IndexError: | |
print 'USAGE: $ killitnow.py <process_name>' | |
sys.exit(0) | |
procs = find_process(tokill) | |
for proc in procs: | |
os.system('kill -9 %s' % proc.pid) | |
print 'killed', tokill, 'having pid', proc.pid, 'cmdline', ' '.join(proc.cmdline) | |
if not procs: | |
print 'cannot find any process like %s' % tokill | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment