Kill All Processes Of Certain Application In Unix/Linux

This is a very helpful command I use regularly.

If your like me, you open several instances of applications like “VI” and leave them in the background, most of the time without meaning to.  Eventually you run a “PS -U” to view all your processes running and realize that theres 10 copies of the application running in the background which you havnt closed. Heres a command to quickly clear that up.

Example: (Multiple VI Processes)
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      4288  0.0  0.1   4908  1372 pts/0    S    Apr20   0:00 su
root      4295  0.0  0.1   4868  1556 pts/0    S+   Apr20   0:00 bash
root      9514  0.0  0.1   4868  1600 pts/1    Ss   Apr21   0:01 -bash
root     10207  0.0  0.0   4344   796 pts/1    T    09:26   0:00 less status.dat
root     11467  1.8  0.1   5452  1568 pts/1    T    10:17   0:00 vi objects.cache
root     11470  0.0  0.1   5452  1580 pts/1    T    10:18   0:00 vi status.dat
root     11473  0.0  0.1   5448  1540 pts/1    T    10:18   0:00 vi nagios.log
root     11480  0.2  0.1   5452  1580 pts/1    T    10:18   0:00 vi retention.dat

root     11481  5.0  0.0   4660   972 pts/1    R+   10:18   0:00 ps -u
root     16094  0.0  0.1   7732  1404 pts/1    T    Apr21   0:00 mail
root     16097  0.0  0.0   4072   724 pts/1    T    Apr21   0:00 more

Lets assume I want to kill all VI processes , but I dont want to kill them one by one.

The Command

ps -u | awk ‘{print $11,$2}’ | grep vi | xargs kill -9

Breakdown
ps -u – Show all processes running under this user
awk {‘print $11,$2′} – Filter this output to only show the 11th and the 2nd columns (The name and the PID)
grep vi – Show only the vi related process
xargs kill -9 – For each result returned, treat it as an object and run the “kill -9″ command against it. (Kill -9 means forcefully kill the process)

Leave a Reply