Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run the script exclusively 3

Status
Not open for further replies.

h3nd

Programmer
Jul 1, 2006
147
AU
Hi guys,

I need to run the script but while the script is running, I don't want any people to run the script again.

Is there anyway to block any other people to do that ?

Thanks guys.
 
You could include a chmod in your script to change the permissions so that it isn't executable whilst running, changing it back as the script ends.

Alan Bennett said:
I don't mind people who aren't what they seem. I just wish they'd make their mind up.
 
I would typically create a lock file for this type of script, i.e. something like this:

[tt]#!/bin/ksh
SCRIPT=$(basename $0)

if [[ -f /var/run/${SCRIPT}.lock ]]
then
echo /var/run/${SCRIPT}.lock exists, exiting
exit 1
fi
echo $$ > /var/run/${SCRIPT}.lock

( rest of script here )

rm /var/run/${SCRIPT}.lock[/tt]

You may also consider adding a trap to remove the lock file in case someone Ctrl-C's the script.

Annihilannic.
 
Hi Anni,

I like your idea, but as you said if someone ctrl-C the script.

How do I remove the lock file :)
 
That's the rm /var/run/${SCRIPT}.lock

at the end of Anni's post.

Alan Bennett said:
I don't mind people who aren't what they seem. I just wish they'd make their mind up.
 
Like I said, add a trap. Here's an example trapping signals 2 (SIGINT, i.e. Ctrl-C) and 15 (SIGKILL, i.e. kill <pid>).

[tt]#!/bin/ksh
SCRIPT=$(basename $0)

trap "rm -f /var/run/${SCRIPT}.lock ; exit 1" 2 15

if [[ -f /var/run/${SCRIPT}.lock ]]
then
echo /var/run/${SCRIPT}.lock exists, exiting
exit 1
fi
echo $$ > /var/run/${SCRIPT}.lock

( rest of script here )

rm /var/run/${SCRIPT}.lock[/tt]



Annihilannic.
 
I'd trap HangUp (and Quit) too, just for safety purpose:
trap "rm -f /var/run/${SCRIPT}.lock; exit 1" 1 2 3 15

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Many ways to do this

Code:
My_pid=$$
print $My_pid >> temp.file
read input_pid < temp.file
if [[ "$input_pid" == "$My_pid" ]] then
   (allow the script to continue)
else
   (exit the script, My_pid was not 1st)
fi

Code:
lockname=/tmp/myscript.lock
    if mkdir "$lockname"
    then
        echo >&2 "aquired lock; continuing"
        # Remove lock directory when script terminates
        trap 'rmdir "$lockname"' 0
        trap "exit 2" 1 2 3 15
    else
        echo >&2 "lock already held"
        echo >&" "another script instance running?"
        exit 1
    fi

both taken from


Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Thx guys,

You save me :)

I love this forum,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top