Is there a flock command on Mac OS X that manages file lock?
http://linux.die.net/man/1/flock
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.
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.
Mac OS is UNIX based with a Darwin Kernel and so the terminal lets you basically enter the commands directly into that UNIX environment.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With