grep the process id and kill all the filtered process

Hi

I want to write a shell script which can find the process id's of all the process and kill them eg:

ps ax | grep rv_
 3015 ?        S      0:00 /home/vivek/Desktop/rv_server
 3020 ?        S      0:00 /home/vivek/Desktop/rv_gps
 3022 ?        S      0:00 /home/vivek/Desktop/rv_show
 3024 ?        S      0:00 /home/vivek/Desktop/rv_game
 3026 ?        S      0:00 /home/vivek/Desktop/rv_mail

[/code]
now kill all process searched(find the child process and kill it first and rest).

How do I write the shell script for the above?

Try this:

kill $(ps ax | grep rv_ | awk '{print $1}')

variant

kill $(ps ax  | awk '/rv_/ {print $1}')
1 Like

The requirement here is different, not multiple instances with same process name. Its different process with different process name. So, explicit pkill should be used with each and every process

When you grep a list of process through a pattern, you can use the same to kill those process.

Experimentation:

$ ps ax | grep rv_
 3382 pts/1    T      0:00 /bin/sh ./rv_server
 3414 pts/1    T      0:00 /bin/sh ./rv_test
 3443 pts/1    T      0:00 /bin/sh ./rv_test
 3450 pts/1    S+     0:00 grep -i rv_
$ pkill -9 rv_
$ ps ax | grep rv_
 3453 pts/1    S+     0:00 grep -i rv_
[3]   Killed                  ./rv_server
[4]-  Killed                  ./rv_test
[5]+  Killed                  ./rv_test
$ ps ax | grep rv_
 3455 pts/1    S+     0:00 grep -i rv_
2 Likes
# ps aux | grep vi | grep -v grep
root      1693  0.0  0.0  74476  1324 pts/1    T    10:33   0:00 vi test
root      1694  0.0  0.0  74344  1312 pts/0    T    10:33   0:00 vi tes
# for i in $(ps aux | grep vi | grep -v grep | awk '{print $2}')
> do
> kill -9 $i
> done
# ps aux | grep vi | grep -v grep
1 Like

Perfectly agree, I missed it. Operand is a pattern and not a literal. Thanks

Thanks it worked.