Killing in the name of

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 = ""
            exe = p.exe
        except Exception,e:
        if name in exe:
            exetime = now - p.create_time
            if exetime > 3600.0:
                    print "killed %s ( %d s of execution time)"%(exe,exetime)
                except Exception,e:
                    print e