On 2/17/06, Michael Moore <[EMAIL PROTECTED]> wrote:
> Hello shell script gurus. I'm sure I'm doing this the hard way...so if
> you could shed some light.
>
> I'm trying to write a shell script that will only run if it's not
> running already, and if it is running, kill the other process. ie:
>
> if ( other copy of me already running)
>    kill both myself and other copy
> else
>    do some stuff
> fi
>
> The script I've got now is named "loopy.sh" and at the moment just has
> a "while [ true ]" loop that does some stuff.
>
> In the script I can check if it's already running with "ps aux | grep
> loopy.sh | wc -l". If it is running, I don't know how to kill it. It
> shows up like this in ps:
> $ ps aux | grep loopy
> liney   1000   0.0  0.1    18644    572 std  S     9:52PM   0:00.00
> bash ./loopy.sh
>
> I don't think "killall bash" would be a good idea because it could
> kill other stuff too. What's the best way to identify and kill the
> right script?
>
> Thank you!
> --
> Michael Moore

Use a PID file.

Something like (I'm doing this off the top of my head--double check my syntax):
------------------------
#!/bin/bash
if [ -e /tmp/mypidfile ] ; then
  kill -9 `cat /tmp/mypidfile`
  rm /tmp/mypidfile
  echo "Already running, AAAaaaa"
  exit -1
fi
echo "I'm the only one running, so now I'll do some stuff..."
echo `(some command to get your own PID)` > /tmp/mypidfile
...
(do stuff)
...
rm /tmp/mypidfile
exit 0
---------------------

~ Nathan

--------------------
BYU Unix Users Group
http://uug.byu.edu/

The opinions expressed in this message are the responsibility of their
author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to