Mac OS X equivalent of Linux flock(1) command

2020-05-21 09:20发布

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

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

8条回答
我命由我不由天
2楼-- · 2020-05-21 09:37

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楼-- · 2020-05-21 09:50

Maybe lockfile could be used as well.

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

查看更多
登录 后发表回答