preventing锁传播
一个简单而看似可靠的方法bash下锁定是:
A simple and seemingly reliable way to do locking under bash is:
exec 9>>lockfile
flock 9
然而,众所周知,bash的传播这样的FD锁分叉所有的东西,包括执行的程序等。
However, bash notoriously propagates such a fd lock to all forked stuff including executed programs etc.
有没有办法告诉bash不能复制FD?这是伟大的锁连接到当程序终止时,不管它如何终止该被删除的FD。
Is there any way to tell bash not to duplicate the fd? It's great that the lock is attached to a fd which gets removed when the program terminates, no matter how it gets terminated.
我知道我可以做的东西,如:
I know I can do stuff like:
run_some_prog 9>&-
不过那是相当繁琐的。
But that is quite tedious.
有没有更好的解决方案?
Is there any better solution?
您可以使用 -o
命令行选项,羊群(1)
(长选项 - 关闭
,这可能是在为自我记录性质的脚本写出更好的)指定文件描述符应通过执行命令羊群之前关闭(1)
:
You can use the -o
command line option to flock(1)
(long option --close
, which might be better for writing in scripts for the self-documenting nature) to specify that the file descriptor should be closed before executing commands via flock(1)
:
-o, --close
Close the file descriptor on which the lock is held
before executing command. This is useful if command
spawns a child process which should not be holding
the lock.