Bash: Using flock to ensure parallel scripts perform an action only once

Consider two or more shell scripts that may be run either by themselves or in parallel, but all rely on a specific setup step that isn’t parallelizable. This may be a network request, a non-idempotent database write, or something else similar.

One easy way to ensure that this action is performed only once in the above setup is to use flock, a simple lock management utility for shell scripts. In the below example we use it to open a lock in /tmp/shared.lock with the file descriptor 200.

#!/usr/bin/env bash
(
    # Only run the below block if we're the first to acquire the lock
    if flock -x -n 200; then
        echo "do the thing";
    # Otherwise wait for the lock to be released and then continue
    else
        flock -x 200;
    fi
) 200>/tmp/shared.lock

# Remainder of the script