Unix questions - exclusive execution of a script

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > November 2006 > exclusive execution of a script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author exclusive execution of a script
Mike

2006-10-31, 7:25 pm

Hi,

What is a good way to insure exclusive execution of a script?

-Mike

Bit Twister

2006-11-01, 1:42 am

On 31 Oct 2006 17:06:44 -0800, Mike wrote:
> Hi,
>
> What is a good way to insure exclusive execution of a script?


Well I would think
touch /tmp/lock.$$
ln -s /tmp/lock.$$ /tmp/lock
if [ $? -ne 0 ] ; then
echo "some other progam is running this script."
fi
should work.

Chris F.A. Johnson

2006-11-01, 1:42 am

On 2006-11-01, Bit Twister wrote:
> On 31 Oct 2006 17:06:44 -0800, Mike wrote:
>
> Well I would think
> touch /tmp/lock.$$
> ln -s /tmp/lock.$$ /tmp/lock
> if [ $? -ne 0 ] ; then
> echo "some other progam is running this script."
> fi
> should work.


That will not prevent the script being run from another terminal.
Try this at the top of the script:

lockfile=/tmp/${0##*/}
ln -s "$0" "$lockfile" 2>/dev/null || exit 13
trap 'rm "$lockfile"' EXIT


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Bit Twister

2006-11-01, 1:42 am

On Tue, 31 Oct 2006 22:35:51 -0500, Chris F.A. Johnson wrote:
> On 2006-11-01, Bit Twister wrote:
>
> That will not prevent the script being run from another terminal.


Frap, forgot to add exit before fi. Nice catch.
Chris F.A. Johnson

2006-11-01, 1:42 am

On 2006-11-01, Bit Twister wrote:
> On Tue, 31 Oct 2006 22:35:51 -0500, Chris F.A. Johnson wrote:
>
> Frap, forgot to add exit before fi. Nice catch.


You also forgot to remove /tmp/lock.$$, and /tmp/lock is not a
good name -- too easily used by other processes.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Bit Twister

2006-11-01, 1:42 am

On Tue, 31 Oct 2006 23:47:53 -0500, Chris F.A. Johnson wrote:

>
> You also forgot to remove /tmp/lock.$$, and /tmp/lock is not a
> good name -- too easily used by other processes.



True, but you have to leave a little work for them to do.

If you want to get picky, I can *source* your script from another script
and bypass your lockout code to get it to continue past ln -s test.

Bit Twister

2006-11-01, 1:42 am

On 31 Oct 2006 17:06:44 -0800, Mike wrote:
> Hi,
>
> What is a good way to insure exclusive execution of a script?


As an oh by the way, when you start playing with the suggestions in
this thread create a small script for testing and create a checklist
of ways the script needs to protect it's self.

I suggest you create a script with something like,



#*************** start of checkit script **************
(exclusive execution checking code here)

echo "hit return to close"; read

exit 0

#*************** end of checkit script **************


Now click up two terminals, and in term 1
chmod +x checkit

../checkit
(hit return key)
../checkit
(do not hit return key at prompt, This assums the prompt came up)

Now in term 2

../checkit
../checkit

and verify no prompt.
Stephane CHAZELAS

2006-11-01, 1:42 am

2006-10-31, 17:06(-08), Mike:
> Hi,
>
> What is a good way to insure exclusive execution of a script?

[...]

if ! mkdir /tmp/.myscript; then
echo >&2 "The script is already running"
exit 1
fi
trap 'rm -rf /tmp/.myscript' EXIT
trap exit INT TERM HUP QUIT
# ...

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com