Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X equivalent of Linux flock(1) command

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

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

like image 680
png Avatar asked May 10 '12 02:05

png


People also ask

Are macOS commands the same as Linux?

In general, both will have the same core commands and features (especially those defined in the Posix standard), but a lot of extensions will be different. For example, linux systems generally have a useradd command to create new users, but OS X doesn't.

Is Mac OS X based on Linux?

You may have heard that Macintosh OSX is just Linux with a prettier interface. That's not actually true. But OSX is built in part on an open source Unix derivative called FreeBSD. And until recently, FreeBSD's co-founder Jordan Hubbard served as director of Unix technology at Apple.

Do UNIX commands work on Mac?

Mac OS is UNIX based with a Darwin Kernel and so the terminal lets you basically enter the commands directly into that UNIX environment.


2 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.

like image 87
sneak Avatar answered Oct 02 '22 07:10

sneak


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);  
like image 26
Ernest Avatar answered Oct 02 '22 09:10

Ernest