问题
Is there a flock command on Mac OS X that manages file lock?
http://linux.die.net/man/1/flock
回答1:
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.
回答2:
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);
回答3:
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.
回答4:
There is no flock
command on OS X, no. 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, you will have to create such a program - by compiling the BSD source yourself, or writing your own equivalent program (perhaps in Perl or Ruby or some other language that exposes flock
as part of its high-level system interface).
If, however, all you need is a way to synchronize access to a file from a shellscript, and you don't have other programs already written trying to do so with flock
, you could use the lockfile
command, which comes with the procmail
package. OS X used to ship with procmail
; it no longer does, but you can install it via e.g. Homebrew.
回答5:
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.
回答6:
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.
回答7:
Maybe lockfile
could be used as well.
http://linux.die.net/man/1/lockfile
回答8:
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.
来源:https://stackoverflow.com/questions/10526651/mac-os-x-equivalent-of-linux-flock1-command