17

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.

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Adam Matan
  • 128,757
  • 147
  • 397
  • 562

2 Answers2

22
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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
14

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.

Yuri Aksenov
  • 141
  • 2
  • 2
    It would be nice to document the cases when this can fail. I suspect it will fail if `$0` contains spaces, and it will fail if `$0` does not contain the path to the running script (e.g. the script is elsewhere but on the `PATH`). – joeytwiddle Jan 25 '16 at 05:37
  • $0 should still have the path if it's in $PATH. One case where it doesn't work is under dash. – steveayre Mar 07 '16 at 13:29
  • exec 200<"$0" # <- double quotes prevent the problem with space characters mentioned above – Gunar Gessner Feb 13 '21 at 17:08
  • In case you're wondering what's so special about the number 200: https://stackoverflow.com/a/13551882/1456173 – Gunar Gessner Feb 13 '21 at 19:13