Is there a flock command on Mac OS X that manages file lock?
相关问题
- How to get the return code of a shell script in lu
- Invoking Mirth Connect CLI with Powershell script
- Xcode debugger displays incorrect values for varia
- Is there a way to report errors in Apple documenta
- Advice for supporting both Mac and Windows Desktop
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- In IntelliJ IDEA, how can I create a key binding t
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
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.
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 asflock
.Your best bet may be to look at porting either the Linux or BSD version of
flock
to OS X.Are you looking for
flock
the command line utility orflock
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 usingflock(2)
should be trivial.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 theflock
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 exposesflock
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 thelockfile
command, which comes with theprocmail
package. OS X used to ship withprocmail
; it no longer does, but you can install it via e.g. Homebrew.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.
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.