This command ps -ef | grep php
returns a list of processes
I want to kill in one command or with a shell script all those processes
Thanks
This command ps -ef | grep php
returns a list of processes
I want to kill in one command or with a shell script all those processes
Thanks
The easiest way to kill all commands with a given name is to use killall
:
killall php
Note, this only sends an interrupt signal. This should be enough if the processes are behaving. If they're not dying from that, you can forcibly kill them using
killall -9 php
The normal way to do this is to use xargs
as in ps -ef | grep php | xargs kill
, but there are several ways to do this.
ps -ef
lists all processes and then you use grep
to pick a few lines that mention "php". This means that also commands that have "php" as part of their command line will match, and be killed. If you really want to match the command (and not the arguments as well), it is probably better to use pgrep php
.
You can use a shell backtick to provide the output of a command as arguments to another command, as in
kill `pgrep php`
If you want to kill processes only, there is a command pkill
that matches a pattern to the command. This can not be used if you want to do something else with the processes though. This means that if you want to kill all processes where the command contain "php", you can do this using pkill php
.
Hope this helps.
You can find its pid (it's on the first column ps
prints) and use the kill
command to forcibly kill it:
kill -9 <pid you found>
Use xargs
:
ps -ef | grep php | grep -v grep | awk '{print $2}' | xargs kill -9
grep -v grep
is exclude the command itself and awk
gives the list of PIDs which are then passed kill
command.
Use pkill php
. More on this topic in this similar question: How can I kill a process by name instead of PID?