Skip to content

Instantly share code, notes, and snippets.

@simahawk
Last active December 23, 2015 15:57
Show Gist options
  • Save simahawk/4195017 to your computer and use it in GitHub Desktop.
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!
#!/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