grep – How to Kill All Processes with Given Name

grepkillprocessps

I run command ps -A | grep <application_name> and getting list of process like this:

19440 ?        00:00:11 <application_name>
21630 ?        00:00:00 <application_name>
22694 ?        00:00:00 <application_name>

I want to kill all process from the list: 19440, 21630, 22694.

I have tried ps -A | grep <application_name> | xargs kill -9 $1 but it works with errors.

kill: illegal pid ?
kill: illegal pid 00:00:00
kill: illegal pid <application_name>

How can I do this gracefully?

Best Answer

pkill -f 'PATTERN'

Will kill all the processes that the pattern PATTERN matches. With the -f option, the whole command line (i.e. including arguments) will be taken into account. Without the -f option, only the command name will be taken into account.

See also man pkill on your system.

Related Question