Linux – Kill Multiple Processes with One Command on Linux/Solaris

killlinuxprocessshell-scriptingsolaris

Is it possible to kill all find process with one command?

I do not want to kill each process as kill -9 25295 , kill -9 11994 , etc.. Rather, what I want is a simple way or command that kill all find process (my target is to perfrom this action on linux and solaris machines).

$ ps -ef | grep find 
root 25295 25290   0 08:59:59 pts/1 0:01 find /etc -type f -exec grep -l 100.106.23.152 {} ; -print
root 11994 26144   0 09:04:18 pts/1 0:00 find /etc -type f -exec grep -l 100.106.23.153 {} ; -print
root 25366 25356   0 08:59:59 pts/1 0:01 find /etc -type f -exec grep -l 100.106.23.154 {} ; -print
root 26703 26658   0 09:00:05 pts/1 0:01 find /etc -type f -exec grep -l 100.106.23.155 {} ; -print

Best Answer

This will work on both Linux and Solaris and do precisely what you need:

pgrep -f 'find /etc'     # verify the listing before proceeding
pkill -9 -f 'find /etc'

In your situation, avoid killall. If you use it on Linux, sooner or later you will mistake the ssh sessions, run it on Solaris, creating unnecessary risk.

The -f option of pgrep/pkill means to match the entire command line. In case you need to match path of the program or script (/var/tmp/test.sh), this works if you had run it with the entire path. To be precise, you only need to escape the . so you need

pkill -9 -f '/var/tmp/test\.sh'

If you have run the same program as ./test.sh you need to kill it as such. See -f option in ps.