[CentOS] Any one have a good example...

James Olin Oden james.oden at gmail.com
Tue Aug 29 19:42:09 UTC 2006


On 8/29/06, mike.redan at bell.ca <mike.redan at bell.ca> wrote:
> Something like this works alright:
>
>
> # Check if process is running already
> SCRIPT_NAME=do_stuff
> pidfile=/tmp/$SCRIPT_NAME.pid
>
>
> if [ -f $pidfile ]; then
>   runpid=`cat $pidfile`
>   if ps -p $runpid; then
>      echo "$SCRIPT_NAME is already running ... stopping"
>      exit 1
>   else
>      echo "$SCRIPT_NAME pid found but process dead, cleaning up and
> starting ..."
>      rm -f $pidfile
>   fi
> else
>   echo "No $SCRIPT_NAME Process Detected ... starting"
> fi
>
> echo $$ > $pidfile
>
As long as you don't have multiple scripts starting at approximately
the same time.   I use this same algorithm sometimes but I always use
flock() because otherwise there is a race condition.  Even using
flock() does not remove the race on the creation of the file, though,
which is why if you really want to remove the race condition you make
sure the file pre-existant (via package delivery or something to that
affect).

At least this is how I understand the problem....james



More information about the CentOS mailing list