Is there a flock command on Mac OS X that manages file lock?
8 Answers
There is a cross-platform flock command here:
https://github.com/discoteq/flock
I have tested it and it works well on OSX as a drop-in replacement for the util-linux flock.

- 1,068
- 1
- 12
- 16
-
How is claiming to succeed but actually failing to take action (behaving as a noop) better than failing explicitly? – Charles Duffy Sep 21 '15 at 02:29
-
2Ahh -- as `-x` is default in that implementation. Okay, makes more sense having read the man page. :) – Charles Duffy Sep 21 '15 at 02:31
-
I filed a feature request to accept `-x` as a no-op here - https://github.com/discoteq/flock/issues/9 – studgeek Jan 10 '17 at 21:04
-
thanks for filing tickets with discoteq/flock, I appreciate it! – Joseph Holsten Jan 12 '17 at 16:03
-
@sneak you can update your response, a generous contributor has recently added the `-x` flag as well as long options. – Joseph Holsten Jan 12 '17 at 20:33
-
Unfortunately two years have passed since the addition of the `-x` option but no new release has been published as of yet. This means that at least with brew you get an outdated version. – megamorf Mar 09 '20 at 17:25
Perl one-liner:
perl -MFcntl=:flock -e '$|=1; $f=shift; print("starting\n"); open(FH,$f) || die($!); flock(FH,LOCK_EX); print("got lock\n"); system(join(" ",@ARGV)); print("unlocking\n"); flock(FH,LOCK_UN); ' /tmp/longrunning.sh /tmp/longrunning.sh
As a script:
#!/usr/bin/perl
# emulate linux flock command line utility
#
use warnings;
use strict;
use Fcntl qw(:flock);
# line buffer
$|=1;
my $file = shift;
my $cmd = join(" ",@ARGV);
if(!$file || !$cmd) {
die("usage: $0 <file> <command> [ <command args>... ]\n");
}
print("atempting to lock file: $file\n");
open(FH,$file) || die($!);
flock(FH,LOCK_EX) || die($!);
print("got lock\n");
print("running command: $cmd\n");
system($cmd);
print("unlocking file: $file\n");
flock(FH,LOCK_UN);
-
2@Yan In the future, you probably should not make code changes to someone else's answer. Feel free to submit your suggested edit as a comment and let the author make the call as to whether he or she wants to change their answer or not (9 times out of 10, if it's a mistake, they'll fix it). Feel free to edit answers for substantial changes for clarity or formatting, but do not change the answer itself. – Rob Feb 04 '13 at 18:56
-
1Note that this Perl emulation is very incomplete - it only supports the form ``flock FILE COMMANDS...`` and not the ``flock FD`` form (neither does it support any of the flock(1) options). – Alex Dupuy Apr 07 '14 at 09:21
I don't believe that the flock
command exists on OS X, but it does exist on BSD which should make it reasonably easy to port to OS X.
The closest that is available is the shlock
command (man page), but it isn't as robust or secure as flock
.
Your best bet may be to look at porting either the Linux or BSD version of flock
to OS X.

- 8,297
- 3
- 35
- 57
macOS does not ship with a flock
command, no, but you can install one via Homebrew (brew install flock
). Which is probably the way to go if you need a shell script that can share a lockable resource with programs that use the flock
system call to manage access to that resource.
If you are just trying to synchronize access to something and don't require compatibility with things already using flock
, you could alternatively install procmail
and use lockfile
instead.

- 91,912
- 16
- 138
- 175
-
2_you should look at the lockfile command (comes with procmail, standard on OS X)._ `lockfile: command not found` on El Capitan (10.11.6) so no it is **not standard**. – Sukima Dec 02 '16 at 17:58
Just for completeness sake, you can compile flock(2) for OSX with some minor changes, i have not run any tests, but basic functionality works.
You can get the source from ftp://ftp.kernel.org//pub/linux/utils/util-linux. You then need to replace some calls to string functions not available on OSX, and you're good to go.
Here: https://gist.github.com/Ahti/4962822 is my modified flock.c of version 2.22.1, you still need the other sources for headers though.

- 1,420
- 1
- 11
- 20
You can install flock
via conda
, for example:
conda create --name flock flock
or
conda install flock
To install conda
, see here.

- 12,024
- 2
- 30
- 47
Are you looking for flock
the command line utility or flock
the feature?
flock(1)
is unavailable on OS X. flock(2)
(the C function for file locking), however is.
Writing a simple command line flock(1)
utility using flock(2)
should be trivial.

- 28,357
- 12
- 85
- 125
-
8"Writing a simple command line flock(1) utility using flock(2) should be trivial." so trivial that you will be kind enough to share one for the non-C dev out there? – Mayeu Jan 31 '19 at 11:03
You cannot write a shell-level flock(1) command for use in shell programming because of how file locking working. The lock is on the descriptor, not on the inode or directory entry.
Therefore, if you implement a shell command that flocks something, as soon as the locking command exits and the shell script moves on to the next command, the descriptor that held the lock disappears and so there is no lock retained.
The only way to implement this would be as a shell builtin. Alternately, you have to rewrite in a programming language that actually supports flock(2) directly, such as Perl.

- 78,834
- 30
- 123
- 180
-
Linux's [flock(1)](http://linux.die.net/man/1/flock) isn't a trivial wrapper around flock(2). – Gilles 'SO- stop being evil' May 13 '12 at 15:18
-
Do I understand it right? The lock is only kept as long as the process is running. So, a simple wrapper around flock(2) would keep the lock only a slong as it is running. In a script the lock would be released before the next command is called, right? – jboi Dec 22 '13 at 10:31
-
2@jboi - the flock(1) command either takes a shell command (which is run while holding the lock) or the number of a file descriptor which should be locked - in the latter case the file descriptor is opened in the caller (if it is a shell script, using ``exec 9>$LOCKFILE`` or similar) and remains open after the flock command exits. As for @tchrist's claim that you cannot write a shell level flock - well, I'd believe (almost) anything he says about Perl, but on this one he's wrong. The flock command runs the locked commands (if passed a filename) or is passed a file descriptor number - it works! – Alex Dupuy Apr 07 '14 at 09:13
-
@tchrist, `flock(1)` isn't a shell builtin even on Linux, so this is obviously untrue. Rather, attaching a lock to a file descriptor applies to that FD even across processes, so it's able to modify the parent's instance as well. – Charles Duffy Sep 20 '15 at 19:43