Last active
May 11, 2022 20:11
-
-
Save marcoagner/9926309 to your computer and use it in GitHub Desktop.
Python script to kill a process by its name
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
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) |
Where do you list the name of the process you want to kill?!?!?! OMG....I've been searching for an hour!!!!
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.
@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
or you could just import the subprocess module, have the user enter a string based input, convert it to int, then via the sys module [ sys.stdin.readline()] or raw_input depending on the version of python, then create a variable to store the input with the 'killall' command, through the subprocess.call() function