Why a lock file alone is not enough
A common first attempt is to create a file such as job.pid at the start of a script and refuse to run if it exists. The approach fails in two ways. If the script crashes, the file stays behind and blocks every later run. And between the check and the creation, a second copy can start, so both pass the test. The file is a flag, not a lock, and the operating system does not enforce it.
What flock does
flock asks the kernel for a lock on a file. The lock is attached to the open file description, the handle that a process holds. When the process exits, for any reason, the kernel closes the handle and releases the lock. A stale lock cannot survive a crash, and two processes cannot both hold an exclusive lock on the same file at the same time.
The usual form for a script is to open the lock file on a numbered descriptor and then lock that descriptor. The file itself can be empty. Its only job is to be a name that every copy of the script agrees on.
exec 9>/workspace/job.lock
flock -n 9 && echo "lock acquired"
flock -n 9 && echo "still free"The first flock -n takes the lock and prints the message. The second call runs in the same shell and asks for the same lock on the same descriptor, which the shell already holds, so it also succeeds. To see a real conflict, a second process must open the file on its own descriptor, as the next example does.
Failing fast with -n
Without -n, flock waits until the lock is free. With -n, it returns status 1 at once if another holder has the lock. Use -n when a second copy should give up and report, and use the blocking form when it should queue behind the first.
exec 9>/workspace/job.lock
flock -x 9
bash -c 'exec 8>/workspace/job.lock; flock -n 8 || echo "busy"'The outer shell holds the exclusive lock on descriptor 9. The inner bash opens the same file on its own descriptor and tries a non-blocking lock. It fails, and the message busy is printed. Locks held by different open file descriptions conflict even when they belong to the same program.
Give the lock file a name that describes the job, such as /var/lock/deploy.lock, and keep it outside the directory that the job rewrites. Deleting the lock file while a process holds it does not release the lock, so a removed lock file can lead to two holders of different files.
Running a command under a lock
For a single command, flock can also run it for you. The -c option takes a command string, runs it with the lock held, and releases the lock when the command ends. This form avoids managing the descriptor yourself.
flock /workspace/job.lock -c 'echo "critical section ran"'The output is critical section ran. The lock was held for exactly the duration of the command, and the status of flock is the status of the command it ran.
Locks and strict mode
A failing flock -n returns status 1, which strict mode treats as an error. That is usually what you want, but a script that prints a friendly message and exits should use an explicit || { echo busy; exit 1; } so that the reason is visible. Combine the lock with a trap only if the script also needs cleanup beyond the lock, because the kernel releases the lock on exit anyway.
Hands-on: hold and test a lock
Run the block below. It holds an exclusive lock on one descriptor, then tries to take the same lock from a child bash. The child should report busy. The lab for this unit writes a locking script and checks that it refuses to run while the lock is held.
exec 9>/workspace/demo.lock
flock -x 9
bash -c 'exec 8>/workspace/demo.lock; flock -n 8 && echo free || echo busy'The output is busy, because the lock belongs to the first shell.