exclusive execution of a script - Unix

This is a discussion on exclusive execution of a script - Unix ; Hi, What is a good way to insure exclusive execution of a script? -Mike...

+ Reply to Thread
Results 1 to 8 of 8

Thread: exclusive execution of a script

  1. exclusive execution of a script

    Hi,

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

    -Mike


  2. Re: exclusive execution of a script

    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.


  3. Re: exclusive execution of a script

    On 2006-11-01, Bit Twister wrote:
    > 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.


    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 |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  4. Re: exclusive execution of a script

    On Tue, 31 Oct 2006 22:35:51 -0500, Chris F.A. Johnson wrote:
    > On 2006-11-01, Bit Twister wrote:
    >> 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.

    >
    > That will not prevent the script being run from another terminal.


    Frap, forgot to add exit before fi. Nice catch.

  5. Re: exclusive execution of a script

    On 2006-11-01, Bit Twister wrote:
    > On Tue, 31 Oct 2006 22:35:51 -0500, Chris F.A. Johnson wrote:
    >> On 2006-11-01, Bit Twister wrote:
    >>> 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.

    >>
    >> That will not prevent the script being run from another terminal.

    >
    > 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 |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  6. Re: exclusive execution of a script

    On Tue, 31 Oct 2006 23:47:53 -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.



    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.


  7. Re: exclusive execution of a script

    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.

  8. Re: exclusive execution of a script

    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

+ Reply to Thread