Created
May 18, 2012 16:50
-
-
Save miebach/2726360 to your computer and use it in GitHub Desktop.
Experimental python script to kill processes on unix-like systems.
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
#!/usr/bin/python -W ignore::DeprecationWarning | |
import sys | |
import os | |
""" | |
Experimental python script to kill processes on unix-like systems. | |
https://gist.github.com/2726360 | |
Looks for all established connections on a given port and kills their processes. | |
First command line argument must be the portnumber. | |
""" | |
SIMULATION=False | |
DEBUG=False | |
#SIMULATION=True | |
#DEBUG=True | |
CR="\r" | |
LF="\n" | |
def freeport(port): | |
if DEBUG: | |
print "Freeing port %s ..." % port | |
# get process numbers of processes with established connections on this port: | |
processlist = established_by_port(port) | |
if DEBUG: | |
print processlist | |
# cut out the process numbers: | |
processnumbers = cut_bytes(processlist,9,14) | |
if (len(processnumbers) == 0): | |
if DEBUG: | |
print "... no processes found. OK." | |
sys.exit() | |
# kill all these processes: | |
if DEBUG: | |
print "killing: %s ..." % processnumbers | |
if not SIMULATION: | |
processes_left=kill(processnumbers) | |
# some hung processes would need -9 and/or sudo: | |
# get process numbers of processes STILL ALIVE with established connections on this port: | |
processlist = established_by_port(sys.argv[1]) | |
if DEBUG: | |
print processlist | |
# cut out the process numbers: | |
processnumbers = cut_bytes(processlist,9,13) | |
if (len(processnumbers) == 0): | |
if DEBUG: | |
print "... all processess killed. OK." | |
sys.exit() | |
# kill all these processes: | |
if DEBUG: | |
print "sudo killing -9: %s ..." % processnumbers | |
if not SIMULATION: | |
processes_left=kill(processnumbers,force=1) | |
if DEBUG: | |
print "Done. %s processes could not be killed." % len(processs_left) | |
def rtrim_crlf(s): | |
""" remove any cr or lf character from the end of s.""" | |
if s == None: | |
return s | |
while 1: | |
if len(s) == 0: | |
return s | |
if s[-1:] in [CR,LF]: | |
s = s[:-1] | |
else: | |
return s | |
def create_cut_list(nfrom,nto): | |
if (nfrom is None) and (nto is None): | |
raise("nfrom and nto cannot both be None") | |
if nfrom == nto: | |
list = "%s" % nfrom | |
elif nfrom is None: | |
list="-%s" % nto | |
elif nto is None: | |
list="%s-" % nfrom | |
else: | |
list="%s-%s" % (nfrom,nto) | |
return list | |
def cut(lines=[],params=""): | |
"""call the cut command on unix-like systems""" | |
cmd = "cut" | |
if params != "": | |
cmd = cmd + " " + params | |
res = call(cmd,lines) | |
return res | |
def cut_by(code,lines=[],nfrom=None,nto=None,complement=0): | |
list = create_cut_list(nfrom,nto) | |
params = "%s %s" % (code,list) | |
if complement: | |
params = params + " --complement" | |
res = cut(lines,params) | |
return res | |
def cut_bytes(lines=[],nfrom=None,nto=None,complement=0): | |
"""the bytes version of the cut command""" | |
return cut_by("-b",lines,nfrom,nto,complement) | |
def cut_byte(lines,n,complement=0): | |
"""only 1 byte""" | |
return cut_bytes(lines,n,n,complement) | |
def cut_fields(lines=[],nfrom=None,nto=None,complement=0): | |
"""the fields version of the cut command""" | |
return cut_by("-f",lines,nfrom,nto,complement) | |
def cut_field(lines,n,complement=0): | |
"""only 1 field""" | |
return cut_fields(lines,n,n,complement) | |
def cut_characters(lines=[],nfrom=None,nto=None,complement=0): | |
"""the chars version of the cut command""" | |
return cut_by("-c",lines,nfrom,nto,complement) | |
def call(cmd,more_lines=[]): | |
""" | |
executes the commandline cmd as a new process p and passes more_lines (which must be a list of lines) | |
as input to p. returns p's output as a list of lines. | |
""" | |
fi,fo = os.popen2(cmd,'t') | |
for l in more_lines: | |
fi.write(l+"\n") | |
fi.close() | |
rescr = fo.readlines() | |
# remove the trailing newline chracters from the resulting lines: | |
res = [] | |
for l in rescr: | |
res.append(rtrim_crlf(l)) | |
fo.close() | |
return res | |
def established_by_port(port): | |
# returns a list of processes with established connections on the port | |
cmd = "lsof -i :%s|grep ESTABLISHED" % port | |
res = call(cmd) | |
return res | |
def kill(process_list=[],force=0): | |
# kill all processes by pids in the list | |
for p in process_list: | |
if force: | |
call("sudo kill -9 %s" % p) | |
else: | |
call("kill -9 %s" % p) | |
if __name__ == '__main__': | |
freeport (sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment