$_ CLI In Depth
Learning progress 0%
lesson robustness traps debugging

Locking with flock

4 min read Module 15: Robust Scripts, Traps & Debugging Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:09

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.

BASH3 lines
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.

BASH3 lines
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.

Tip

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.

BASH1 lines
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.

BASH3 lines
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.

Terminal lab · 2 steps · real Linux in your browser

Hold a lock and run a command under it

Write a script that refuses to run while another process holds its lock, then wrap a command with flock -c.

01
Refuse to run while locked
Create lock.sh in /workspace. It opens /workspace/job.lock on file descriptor 9, takes a non-blocking lock, prints busy and exits 1 if the lock is taken, and otherwise prints locked: ``bash cat > lock.sh <<'EOF' #!/bin/bash exec 9>/workspace/job.lock flock -n 9 || { echo busy; exit 1; } echo locked EOF `` Run it once, then hold the lock from another shell and run it again.
Show hint for step 1
The exec line opens the lock file for writing on a descriptor. The flock -n call fails at once when another open file holds the lock.
02
Run a command under flock -c
Create wrap.sh in /workspace. Use flock with the -c option so that it runs a single echo while holding /workspace/job.lock: ``bash cat > wrap.sh <<'EOF' #!/bin/bash flock /workspace/job.lock -c 'echo "critical section ran"' EOF `` Run it. The lock is released when the command finishes.
Show hint for step 2
The lock file is the first argument and -c takes the command as one quoted string.
Check yourself Question 1 of 4
Concept Check Non-blocking flock recall

What does flock -n do when another process already holds the lock?

Recap & Key Takeaways

A lock file that is only checked for existence can be stale after a crash and can be raced by two starting copies, so use a real kernel lock.
flock attaches the lock to an open file descriptor, so the kernel releases it automatically when the process exits.
flock -n fails at once when another holder has the lock, while the plain form waits for it to be free.
flock -c runs a single command under the lock and releases it when the command ends.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue