8-Bit Borges
8-Bit Borges

Reputation: 10033

Kill a sequence of python processes

I know I can kill a process at terminal by doing:

$ kill -9 666 

where 666 is the PID.

but if I have ran n workers and want to kill all n processes, like so:

$ ps

  PID TTY           TIME CMD
  415 ttys000    0:00.09 -bash
 4356 ttys000    0:00.85 /Users/me/bin/python 
 4359 ttys000    0:03.69 /Users/me/bin/python 
 4360 ttys000    0:03.25 /Users/me/bin/python 
 4361 ttys000    0:03.11 /Users/me/bin/python 
 4362 ttys000    0:02.05 /Users/me/bin/python 
 4363 ttys000    0:01.47 /Users/me/bin/python 
 4364 ttys000    0:01.47 /Users/me/bin/python 
 4365 ttys000    0:01.47 /Users/me/bin/python 
 4366 ttys000    0:01.47 /Users/me/bin/python 
 4367 ttys000    0:03.11 /Users/me/bin/python 
 4368 ttys000    0:01.48 /Users/me/bin/python 
 4369 ttys000    0:01.49 /Users/me/bin/python 
 4370 ttys000    0:01.47 /Users/me/bin/python 
 4371 ttys000    0:01.48 /Users/me/bin/python 
 4372 ttys000    0:01.47 /Users/me/bin/python 
 4373 ttys000    0:02.68 /Users/me/bin/python 
 4374 ttys000    0:03.11 /Users/me/bin/python 

is there a command to kill them all at once?

Upvotes: 0

Views: 100

Answers (1)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

You may simply kill all your processes based on it's name using below command:

pkill -9 -f <process-name>

For example, in your case the command will be:

pkill -9 -f /Users/me/bin/python

Another alternative solution will be to grep the result of ps command based on the CMD parameter and extract there pid using awk, and then kill those with kill -9 as:

kill -9 `ps | grep <process-to-kill> | awk '{print $1}'`

For your case, in order to kill all the "/Users/me/bin/python" processes, the command will be:

kill -9 `ps | grep "/Users/me/bin/python" | awk '{print $1}'`

Here, ps | grep "/Users/me/bin/python" | awk '{print $1}' will return you the list of all your process id as:

 4356 
 4359 
 ...
 4373
 4374

and kill -9 will all the processes belonging to above pids.

Upvotes: 1

Related Questions