Killing in the name of

1 minute read Published:

Using psmisc in python i’ve found this function quite useful in many projects i’ve been working lately.

Basically it will kill all the process older than one hour that match “name”

All those try except are just to avoid permission denied on kill or on name check.

def killing_in_the_name_of(name):
    now = time.time()
    for p in psutil.get_process_list():
        exe = ""
        try:
            exe = p.exe
        except Exception,e:
            pass
        if name in exe:
            exetime = now - p.create_time
            if exetime > 3600.0:
                try:
                    p.kill()
                    print "killed %s ( %d s of execution time)"%(exe,exetime)
                except Exception,e:
                    print e