I am running a Python program service_host.py, which started multiple processes. And when I use ps -ef | grep python
to check the pids, it shows a lot:
congmin 26968 22897 0 Jun20 ? 00:00:00 python service_host.py
congmin 26969 22897 0 Jun20 ? 00:00:00 python service_host.py
congmin 26970 22897 0 Jun20 ? 00:00:00 python service_host.py
congmin 26971 22897 0 Jun20 ? 00:00:00 python service_host.py
congmin 26972 22897 0 Jun20 ? 00:00:00 python service_host.py
congmin 26973 22897 0 Jun20 ? 00:00:00 python service_host.py
congmin 26974 22897 0 Jun20 ? 00:00:00 python service_host.py
congmin 26975 22897 0 Jun20 ? 00:00:00 python service_host.py
What's the best way to kill all these processes at once? I am on Linux and don't want to kill one by one through the process ID. Is there a way to kill them by the Python name 'service_host.py'? I tried this but it didn't kill at all:
import psutil
PROCNAME = "service_host.py"
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == PROCNAME:
proc.kill()
Is there a terminal command that can do this job easily?
Using htop
instead you’ll be able to filter with F4
the processes service_host
instead and have a look at the whole tree with F5
. From there you can kill
them all at once with F9
...
Follow these simple 3 steps:
htop
F4
and type service_host
to filter the process to killF5
to sort processes as a tree. This organizes child processes to its primary process ID (PID)F9
for kill options15 SIGTERM
(signal terminate) to gracefully request to stop the proccess(es) or 9 SIGKILL
(signal kill) to forcefully shut down the process(es) and finally press enterI usually go with 9 SIGKILL
without worrying too much, but it's up to you and what you're doing within your python script!