Subject: RE: kill in bash script
To: 'Bill Schoolcraft' <helix@mayn.de>
From: Rose, Brian <Brian.Rose@icn.siemens.com>
List: netbsd-help
Date: 11/13/2002 10:37:25
I made a generic killer BASH script to kill processes. It is pretty
much the same as the one Bill sent, except that it has some code to
keep from killing itself. I don't know if this is really needed, but
I put it in there for completeness.

You'll have to reconstruct the PIDS= line because Microsoft Outlook
(where I'm sending this email from) truncates lines. I added a CR
before the second -e sed statement. If You put that all on one line,
then you should not have any problems.


#!/bin/bash

if [ ! -z "$1" ]; then 
  PROCESS=$1

  # ps - list the process
  # grep - filter on the process name
  # sed - filter the killer script which has the process name as
  #          an argument
  #       find the target (up to 6 digit pid)
  # grep - remove blank lines
  #
  PIDS=`ps -ax | grep $PROCESS | sed -e "s/.*killp.*//" 
          -e "s/ *\([0-9]\{1,6\}\) .*/\1/" | grep -v "^$"`
  
  if [ ! -z "$PIDS" ]; then
    echo Killing $PROCESS pid=$PIDS
    kill -9 $PIDS
  else
    echo No process $PROCESS in process list
  fi

else
  echo Useage : $0 process_name
fi


> -----Original Message-----
> From: Bill Schoolcraft [mailto:bill@linuxcare.com]
> Sent: Wednesday, November 13, 2002 12:36 PM
> To: joerch
> Cc: netbsd-help@netbsd.org
> Subject: Re: kill in bash script
> 
> 
> joerch wrote:
> > 
> > On Wed, Nov 13, 2002 at 02:06:58PM +0000, Carlo Smith wrote:
> > >
> > > Thats the way to manualy find the pid and then kill it 
> ... but how do I pick
> > > out the pid in a script?
> > >

> Or, you can replace the "netscape" with "$1" and then just run the
> script from the command line using ANY service name like this:
> 
> $> goodbye netscape
> 
> The word netscape would be recognized as $1
> 
> #!/bin/sh
> #
> for i in `ps -auxw|grep "$1" |grep -v grep|awk '{print $2}'`; 
> do kill -9
> $i ; done
> 
> --
> Bill Schoolcraft, Unix/Linux System Engineer
>