Skip to content

Instantly share code, notes, and snippets.

@marcoagner
Last active May 11, 2022 20:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marcoagner/9926309 to your computer and use it in GitHub Desktop.
Save marcoagner/9926309 to your computer and use it in GitHub Desktop.
Python script to kill a process by its name
import os, signal
def check_kill_process(pstring):
for line in os.popen("ps ax | grep " + pstring + " | grep -v grep"):
fields = line.split()
pid = fields[0]
os.kill(int(pid), signal.SIGKILL)
@ageron
Copy link

ageron commented Jan 16, 2019

You can use psutil's process_iter() to list processes, and use each process's name() and cmdline() method to get its name and command line arguments.
For example, this gist implements a few functions that list processes by name or by filtering on their command line arguments.

@datalifenyc
Copy link

@marcoagner Perfect solution for my selenium issue with driver.quit() not terminating chromedriver.

macOS: Catalina  
Python: 3.7.4
ChromeDriver: 80.0.3987.106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment