macos Mac OS X 等效于 Linux flock(1) 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10526651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:09:20  来源:igfitidea点击:

Mac OS X equivalent of Linux flock(1) command

macosshellfile-locking

提问by png

Is there a flock command on Mac OS X that manages file lock?

Mac OS X 上是否有管理文件锁定的 flock 命令?

http://linux.die.net/man/1/flock

http://linux.die.net/man/1/flock

回答by sneak

There is a cross-platform flock command here:

这里有一个跨平台的 flock 命令:

https://github.com/discoteq/flock

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.

我已经对它进行了测试,它在 OSX 上运行良好,可以替代 util-linux flock。

回答by Ernest

Perl one-liner:

Perl 单行:

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

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: ##代码## <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); 

回答by mttrb

I don't believe that the flockcommand exists on OS X, but it does exist on BSD which should make it reasonably easy to port to OS X.

我不相信该flock命令存在于 OS X 上,但它确实存在于 BSD 上,这应该使移植到 OS X 变得相当容易。

The closest that is available is the shlockcommand (man page), but it isn't as robust or secure as flock.

可用的最接近的是shlock命令(手册页),但它不如flock.

Your best bet may be to look at porting either the Linux or BSD version of flockto OS X.

最好的办法可能是考虑将 Linux 或 BSD 版本移植flock到 OS X。

回答by Mark Reed

There is no flockcommand on OS X, no. If you need a shell script that can share a lockable resource with programs that use the flocksystem 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 flockas part of its high-level system interface).

flockOS X 上没有命令,没有。如果您需要一个可以与使用flock系统调用来管理对该资源的访问的程序共享可锁定资源的 shell 脚本,您将必须创建这样一个程序 - 通过自己编译 BSD 源,或编写您自己的等效程序(也许在 Perl 或 Ruby 或其他一些flock作为其高级系统接口的一部分公开的语言中)。

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 lockfilecommand, which comes with the procmailpackage. OS X used to ship with procmail; it no longer does, but you can install it via e.g. Homebrew.

但是,如果你需要的是一种方法来同步访问从shell脚本文件,而你没有已经写其他程序试图这样做有flock,你可以使用的lockfile命令,它自带的procmail包。OS X 曾经附带procmail; 它不再存在,但您可以通过例如Homebrew安装它。

回答by Ahti

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.

为了完整起见,您可以为 OSX 编译 flock(2) 并进行一些小的更改,我没有运行任何测试,但基本功能有效。

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.

您可以从ftp://ftp.kernel.org//pub/linux/utils/util-linux获取源代码。然后,您需要替换一些对 OSX 上不可用的字符串函数的调用,您就可以开始了。

Here: https://gist.github.com/Ahti/4962822is my modified flock.c of version 2.22.1, you still need the other sources for headers though.

此处:https: //gist.github.com/Ahti/4962822 是我修改后的 flock.c 版本 2.22.1,不过您仍然需要其他来源的标题。

回答by jayeff

Maybe lockfilecould be used as well.

也许lockfile也可以使用。

http://linux.die.net/man/1/lockfile

http://linux.die.net/man/1/lockfile

回答by Mahmoud Al-Qudsi

Are you looking for flockthe command line utility or flockthe feature?

您是在寻找flock命令行实用程序还是flock该功能?

flock(1)is unavailable on OS X. flock(2)(the C function for file locking), however is.

flock(1)在 OS X 上不可用。flock(2)(用于文件锁定的 C 函数),但是.

Writing a simple command line flock(1)utility using flock(2)should be trivial.

使用编写一个简单的命令行flock(1)实用程序flock(2)应该是微不足道的。

回答by tchrist

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.

由于文件锁定的工作方式,您不能编写用于 shell 编程的 shell 级flock(1) 命令。锁在描述符上,而不是在 inode 或目录条目上。

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.

因此,如果您实现了一个使某些东西聚集的 shell 命令,一旦锁定命令退出并且 shell 脚本移动到下一个命令,持有锁的描述符就会消失,因此不会保留任何锁。

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.

实现这一点的唯一方法是作为内置的 shell。或者,您必须使用实际直接支持flock(2)的编程语言(例如 Perl)进行重写。