Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

So I have a bash scripts that does some things (pull from github, delete folders .. etc ..) pretty basic stuff. I usually run it whenever I need a new version of the web app on the server.

The thing is there isn't anything that stopping this script from running simultaneously by different people (almost all developers connect to this server and they execute the same command).

So far, it didn't happen that the script is run by more than 1 developer at a time.

How do I make sure that this script cannot be run if it's already running by another user? Folder locking or checking if the script is running by another user (is this doable?) something else?

All users that can run this script are sudoers.

EDIT: Well after checking my colleagues turns out that not all of them are using the script .. some of them are executing the commands by hand so I need to lock the folder itself.

share|improve this question

marked as duplicate by Anthon, Zelda, Timo, slm, manatwork Mar 11 at 16:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
You want to look up mutex locking. Two answers below give methods. –  mikebabcock Mar 11 at 13:26
add comment

4 Answers 4

up vote 5 down vote accepted

flock - Manages locks from shell scripts

man flock

eg

flock -x lockfile -c command

share|improve this answer
    
thank you, will look into it. –  Ali Mar 11 at 12:57
    
will be using flock, thank you. –  Ali Mar 11 at 14:22
add comment

Putting these line inside your script also works. (Sorry, I forgot I defined mutex as a function).

mutex() {
    if ( set -o noclobber; echo "$$" > "$1") 2> /dev/null; then
            trap "rm -f \"$1\"; exit \$?" INT TERM EXIT KILL
            return 0
    else
            return 1
    fi
}

mutex .LOCKFILE || { echo "Another instance of $0 still exists"; exit 1; }
share|improve this answer
    
I like that, thanks :D. Going to try it in a bit. –  Ali Mar 11 at 13:27
    
Do you have a link or reference for this program? –  glenn jackman Mar 11 at 13:48
add comment

Using a lock directory is a nice atomic operation:

lockdir="/tmp/lock.$(basename "$0")"

if ! mkdir "$lockdir" >&/dev/null; then
    echo "lock directory already exists" >&2
    exit 1
fi

...

# remember to clean up. Use `trap` for robustness
rmdir "$lockdir"
share|improve this answer
add comment

You could add some precursor code that checks whether an instance of your script is running. The check is based on your running

ps ax | grep bash | grep hello.sh

In this case, someone is running your script "hello.sh" and the process ID is 78775:

78775 s003  R+     0:02.40 bash ./hello.sh

If no one is running your script, the output line that gets returned is blank.

Your precursor code should be able to spot blank output when you run the ps command and prevent the script from running, either through stoppage or through a set or random delay - your choice.

In summary, you are running the meat of your script as a singleton.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.