Archive

Posts Tagged ‘kill process by name’

psutil: a cross-platform process and system utilities module

November 30, 2011 Leave a comment

psutil is a module providing an interface for retrieving information on all running processes and system utilization (CPU, disk, memory) in a portable way by using Python, implementing many functionalities offered by command line tools such as: ps, top, df, kill, free, lsof, netstat, ifconfig, nice, ionice, iostat, iotop, uptime, tty. It currently supports Linux, Windows, OSX and FreeBSD both 32-bit and 64-bit with Python versions from 2.4 to 3.3 by using a single code base.” (source)

Example
Let’s kill a process by name:

# http://stackoverflow.com/questions/2940858/kill-process-by-name-in-python
import psutil

PROCNAME = 'chromedriver'

def kill_chromedriver():
    for proc in psutil.process_iter():
        if proc.name == PROCNAME:
            proc.kill()
Categories: python Tags: , ,