Re: ksh script question



> # 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



Relevant Pages

  • Re: cron jobs not done during sleep
    ... First thing in your script look for a flag file indicating last time ... If the flag file does exist compare dates. ... they must first drive mad. ...
    (freebsd-questions)
  • Re: Newbie to logon scripts - how to make a script run once only?
    ... script can check for the existence. ... The logon script would check for the existence of the flag file. ... nature of the flag file depends on the deployment. ...
    (microsoft.public.windows.server.scripting)
  • Re: [SLE] Pullin my hair out... arrrgh
    ... > story of the donkey that needed a 2x4 in the head to get its attention so it ... Alternatively, what the original poster said, is to have the script run ... Ie, the first time it would run at 00:00 hours, and would save a flag file ... the flag file, notice the time lapse not being 18 hours, ant exit. ...
    (SuSE)
  • Re: Application to be called - runas Administrator
    ... you can run the script through MS script encoder, ... >I don't believe you can compile a vbscript - this information was passed on ... This flag file would be checked for presence in the ... >> beginning of the compiled program to bypass if it had already been run on ...
    (microsoft.public.scripting.vbscript)
  • Re: [Full-Disclosure] Cleanining viruses from netware
    ... Another approach is to use a login script that runs the disinfection ... run, run the script and then *IF* successful, create the flag file. ... infected machines and remove them. ...
    (Full-Disclosure)