How it works...

Let's understand our script in detail:

  1. The mylock.sh script reuses a couple of concepts that we are already familiar with: traps and symbolic links. We know that if a trap is called or rather, it catches a particular signal, it can clean up a lock file (as is the case in this script). Symbolic links are used since they can survive atomic operations over network file systems. If a file is present at the LOCKFILE location, then a lock is present. If the LOCKFILE is absent, the doors are open.
  2. When we run mylock.sh, we will get the following because no lock file exists yetincluding any temporary ones:
$ bash mylock.shCreated tmp lockDoor was left unlocked
  1. Since the preceding script exited correctly, the SIGKILL signal was ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.