Unix Programming - How to write a script to kill more than one process matching with grep

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2007 > How to write a script to kill more than one process matching with grep





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to write a script to kill more than one process matching with grep
mullin

2007-08-23, 1:27 pm

Let's say that have a program that will be run more than one instance
on Solaris, how can I use "kill" command to delete them all if I'm
using bash shell.

If I run this command:
/usr/ucb/ps -auxwww | grep app_name | cut -f1-3 -d ' '

it will return something like this:
userA: 12345
userA: 12340

where userA is the userid running the program and 2nd one is the PID.

But, how can I write the sh script to kill them all?

Bit Twister

2007-08-23, 1:27 pm

On Thu, 23 Aug 2007 05:23:57 -0700, mullin wrote:
> Let's say that have a program that will be run more than one instance
> on Solaris, how can I use "kill" command to delete them all if I'm
> using bash shell.
>
> If I run this command:
> /usr/ucb/ps -auxwww | grep app_name | cut -f1-3 -d ' '
>
> it will return something like this:
> userA: 12345
> userA: 12340
>
> where userA is the userid running the program and 2nd one is the PID.
>
> But, how can I write the sh script to kill them all?


First thing I would do is check if pkill is installed.

Another option is something like

/usr/ucb/ps -auxwww | grep app_name | cut -f1-3 -d ' ' > kill.list
while read line
do
echo kill $line <==== remove echo to do the kill.
done < kill.list
rm kill.list


for extra points, read
http://tldp.org/LDP/abs/html/index.html
Scott Lurndal

2007-08-23, 1:27 pm

mullin <mullin.yu@gmail.com> writes:
>Let's say that have a program that will be run more than one instance
>on Solaris, how can I use "kill" command to delete them all if I'm
>using bash shell.
>
>If I run this command:
>/usr/ucb/ps -auxwww | grep app_name | cut -f1-3 -d ' '
>
>it will return something like this:
>userA: 12345
>userA: 12340
>
>where userA is the userid running the program and 2nd one is the PID.
>
>But, how can I write the sh script to kill them all?
>


kill -SIGNAL $(ps -ef | grep app_name | cut -c9-14)

adjust the -c9-14 as appropriate for Solaris ps -ef output.

Substitute appropriate signal name or number for SIGNAL.

scott
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com