>_ IzzyLab

Hands-on task

This task is step 25 of the course “Bash scripts: from a loop to a robust script”

Don't let the script run twice

hard~12 min

The script /usr/local/bin/job.sh runs on a schedule and sometimes starts before the previous run has finished — so the job runs in parallel with itself. Modify the script using a flock lock so that a second concurrent run exits immediately with a non-zero code without doing anything, while the first one keeps working. Leave the payload (writing a line to /var/lib/job/runs.log and the pause) as it is. Open the script in the Editor tab above the terminal: the /usr/local/bin directory is open there.

What you'll practise:
  • flock
  • locking
  • file descriptors
  • concurrent runs
  • exec N>

Topic breakdown

flock takes an advisory lock on an open file descriptor. Its key property is that the kernel releases the lock when the descriptor is closed, i.e. on any process exit, including a crash; so, unlike home-grown pid files, it never leaves "stuck" locks. The -n (non-blocking) flag makes it return an error instead of waiting, which gives exactly the "the second run doesn't execute" semantics. The idiom exec 9>file opens the descriptor for the whole lifetime of the script, so the lock is held exactly as long as the work goes on. Keep in mind that child processes inherit open descriptors: if the script starts something in the background, that child keeps holding the lock even after the script itself exits — such commands are started with the descriptor closed (9>&-).

What's checked

  1. script uses flock
  2. a second concurrent run fails
  3. the payload ran exactly once

What it looks like