grep the process id and kill all the filtered process | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

grep the process id and kill all the filtered process

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-19-2010
vivek_naragund vivek_naragund is offline
Registered User
 
Join Date: May 2010
Last Activity: 21 May 2010, 8:32 AM EDT
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
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:

Code:
Code:
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

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?

Moderator's Comments:
Use code tags please, ty.

Last edited by vivek_naragund; 05-21-2010 at 07:17 AM.. Reason: path
Sponsored Links
    #2  
Old 05-19-2010
pseudocoder's Avatar
pseudocoder pseudocoder is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 17 February 2012, 6:42 AM EST
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts
Try this:

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

Sponsored Links
    #3  
Old 05-19-2010
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by pseudocoder View Post
Try this:

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


variant

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

The Following User Says Thank You to matrixmadhan For This Useful Post:
pseudocoder (05-19-2010)
    #4  
Old 05-20-2010
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by thegeek View Post
i hope pkill can also help you out: 4 Ways to Kill a Process kill, killall, pkill, xkill (link removed)

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
Sponsored Links
    #5  
Old 05-20-2010
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
Quote:
Originally Posted by matrixmadhan View Post
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:


Code:
$ 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_

The Following 2 Users Say Thank You to thegeek For This Useful Post:
matrixmadhan (05-21-2010), vivek_naragund (05-21-2010)
Sponsored Links
    #6  
Old 05-21-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 23 July 2014, 7:58 AM EDT
Location: istanbul
Posts: 1,671
Thanks: 2
Thanked 285 Times in 278 Posts

Code:
# 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


Code:
# for i in $(ps aux | grep vi | grep -v grep | awk '{print $2}')
> do
> kill -9 $i
> done


Code:
# ps aux | grep vi | grep -v grep

The Following User Says Thank You to ygemici For This Useful Post:
vivek_naragund (05-21-2010)
Sponsored Links
    #7  
Old 05-21-2010
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by thegeek View Post
When you grep a list of process through a pattern, you can use the same to kill those process.

Experimentation:


Code:
$ 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_

Perfectly agree, I missed it. Operand is a pattern and not a literal. Thanks
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell Script to Kill Process(number of process) Unix/Solaris jonnyvic Shell Programming and Scripting 2 12-03-2009 01:44 PM
Kill a process from a grep ierpe Shell Programming and Scripting 3 11-04-2008 04:44 AM
how to start a process and make it sleep for 5 mins and then kill that process shrao Shell Programming and Scripting 6 03-27-2007 12:54 PM
When kill doesnt work, how to kill a process ? VijayHegde UNIX for Advanced & Expert Users 3 05-12-2006 04:24 PM
How to kill a process and get process id jmdiaz UNIX for Dummies Questions & Answers 5 07-31-2001 03:35 AM



All times are GMT -4. The time now is 06:32 PM.