Adam Matan
Adam Matan

Reputation: 136351

bash flock: exit if can't acquire lock

The following lock mechanism is used for preventing a cron job from running concurrently:

#!/bin/bash

echo "Before critical section"
(
    flock -e 200
    echo "In critical section"
    sleep 5
) 200>/tmp/blah.lockfile
echo "After critical section"

When running two instances together, the later waits until the first finishes, and then runs. This can cause backlogs of scripts waiting to run.

How do I alter this script so that if flock can't acquire the lock, it terminates the script? I've tried -n without success.

Upvotes: 18

Views: 13940

Answers (3)

Majal
Majal

Reputation: 1741

One-liner example from the flock manpage

Put this on the top of your script:

[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :

Explanation:

This is useful boilerplate code for shell scripts. Put it at the top of the shell script you want to lock and it’ll automatically lock itself on the first run. If the environment variable $FLOCKER is not set to the shell script that is being run, then execute flock and grab an exclusive non-blocking lock (using the script itself as the lock file) before re-execing itself with the right arguments. It also sets the FLOCKER environment variable to the right value so it doesn’t run again.

Upvotes: 0

Yuri Aksenov
Yuri Aksenov

Reputation: 141

We use exclusive lock on the script file itself, $0 is the name of command file.

exec 200<$0
flock -n 200 || exit 1

The whole solution is in two lines of code. But the trick is to open $0 for reading and then obtain exclusive lock for it.

Upvotes: 14

John Zwinck
John Zwinck

Reputation: 249394

flock -n -e 200 || exit 1

flock -n tells you it failed by returning a failure code (something other than zero). You could instead do set -e at the top of your script to make it exit when it sees any unchecked error.

Depending on your application, you might want to exit 0 to indicate success when the lock can't be acquired.

Upvotes: 22

Related Questions