help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] signal safe mutex in bash


From: Peng Yu
Subject: [Help-bash] signal safe mutex in bash
Date: Sat, 20 Apr 2013 13:31:19 -0500

Hi,

I see discussions on mutex based on lockfile (the following is for sh,
but should be adapted for bash easily). But it is not safe with
respect to signals like SIGINT SIGTERM. To do so, I know trap should
be used. But I don't know the correct way. Could anybody let me the
correct way to combine lockfile and trap to make the a mutex that is
safe with respect to signals? Thanks.

http://unix.stackexchange.com/questions/70/what-unix-commands-can-be-used-as-a-semaphore-lock

#!/bin/sh
LOCKFILE=$HOME/.myscript/lock
mkdir -p `dirname $LOCKFILE`

echo Waiting for lock $LOCKFILE...
if lockfile -1 $LOCKFILE
then
    # Do protected stuff here
    echo Doing protected stuff...

    # Then, afterward, clean up so another instance of this script can run
    rm -f $LOCKFILE
else
    echo "Failed to acquire lock!  lockfile(1) returned $?"
    exit 1
fi

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]