Re: ksh script question
- From: Yves Dorfsman <yves@xxxxxxxxx>
- Date: Thu, 26 Jan 2006 13:37:24 -0700
> # Begin Script
> semfn=/tmp/`basename $0`.${sid}.RUNNING # Semaphore Filename, Are we
> running already?
> if [ -f $semfn ]
> then
> echo "%-Warn, $0 is Already Running, to continue: rm $semfn"
> return 1
> fi
> touch $semfn
>
> # ... Processing stuff
>
>
> rm $semfn # We are done, so delete semaphore
> # End Script
Watch, you could have a race condition here. You could have two scripts
starting almost exactly at the same time, and have the second one being
done with the "if test" before the first one has created the file.
The way I do it is like this:
FLAG=/tmp/something.flag
# remove the flag file when we're done
trap "rm $FLAG" 0 1 2 15
# Now check if this script is already running
set -o noclobber
cat $$ >$FLAG
set +o noclobber
if [[ $? -ne 0 ]] ; then
print -u2 "script already running\nAborting...\n"
exit 1
fi
you can obviously do variations, like a loop with a sleep etc....
Yves.
----
Yves Dorfsman yves@xxxxxxxxx
http://www.cuug.ab.ca/dorfsmay
http://www.SollerS.ca
- References:
- Re: ksh script question
- From: Gene Sais
- Re: ksh script question
- Prev by Date: Re: ksh script question
- Next by Date: Re: ksh script question
- Previous by thread: Re: ksh script question
- Next by thread: Re: ksh script question
- Index(es):
Relevant Pages
|
|