4

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

geekInside
  • 588
  • 2
  • 8
  • 18

5 Answers5

4

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
Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
2

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.

Mats Kindahl
  • 1,863
  • 14
  • 25
1

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>
1

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.

P.P
  • 117,907
  • 20
  • 175
  • 238
0

Use pkill php. More on this topic in this similar question: How can I kill a process by name instead of PID?

Community
  • 1
  • 1
Gregor
  • 4,306
  • 1
  • 22
  • 37