I would like to synchronize two Bash scripts via a file lock. How to do this? Zsh has zsystem flock
call, but is there a more "shellish" method that's also available to Bash?
Asked
Active
Viewed 2.0k times
6

Itzie
- 139
1 Answers
11
Apart from locking the file itself, you could create a file used as a lock (a lockfile). There are a bunch of utilities for this, e.g. procmail
has lockfile
, and in Debian, the liblockfile-bin
package has dotlockfile
. Procmail should be common enough to be available on most systems.
But basically it comes down to something like:
tempfile=$(mktemp ./lock.XXXX)
lockfile=./lockfile
if ln $tempfile $lockfile ; then
echo got it
# do something
rm $lockfile # after you're done
else
echo did not succeed
fi
rm $tempfile
ln
will not clobber the target if it already exists, unlike something like echo > file
, instead you get an error you can check.
NOTE: if you want to do locks over NFS, use some tool made for that purpose. The semantics of concurrent access to files over NFS are... interesting to say the least.

ilkkachu
- 138,973
-
This indeed allows atomic lock, but cannot cleanup. If script is interrupted, the lock stays in place and there's no way for an automatic cleanup. One solution would be to write the caller PID in the file (which cannot work long-term as PIDs are eventually reused, but at least makes a stale lock removal possible). – Stéphane Gourichon Sep 09 '19 at 12:27
-
@StéphaneGourichon, yes, though in Bash you can
trap 'rm $lockfile' EXIT
, which should remove the lock file on exit unless the shell is somehow prevented from running the exit trap. If that's still an issue, then perhaps the flock` utility mentioned in the linked question should be used. – ilkkachu Sep 09 '19 at 12:45 -
@Michał Krzysztof Feiler, (I don't know if the ping works)
ln -T
is a GNU feature, and even with it, the locking won't work if the lock file is a directory instead. Though with-T
,ln
would give an error, but in any case the lockfile needs to have a known name, and it must be possible to create it. Anyway, when you write edits, there's no need to mark them with "EDIT", just edit the text to include the new information in a natural way. – ilkkachu Oct 09 '19 at 13:33
man flock
, might be all you need. – yarl Sep 10 '16 at 08:32