bash shell 脚本中是否有互斥量/信号量机制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6870221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any mutex/semaphore mechanism in shell scripts?
提问by Mandar Pande
I'm looking for mutex/semaphore/concurrency mechanism in shell script. Consider following situation: Unless "a" user does not close the shared file, "b" user should not able to open/update it. I'm just wondering how to implement mutex, semaphore, critical sections, etc. in shell scripting.
我正在寻找 shell 脚本中的互斥量/信号量/并发机制。考虑以下情况:除非“a”用户不关闭共享文件,“b”用户应该无法打开/更新它。我只是想知道如何在 shell 脚本中实现互斥、信号量、临界区等。
Which is the easiest way to implement locking mechanism [file level] in shell scripting?
在 shell 脚本中实现锁定机制 [文件级别] 的最简单方法是什么?
采纳答案by shellter
See BashFAQand ProcessManagmentfor discussions on file locking in Bash.
有关Bash 中文件锁定的讨论,请参阅BashFAQ和ProcessManagment。
Tagging your question as shell (only) limits the number of people that can help you. You may want to add unix, ksh, bash.
将您的问题标记为 shell(仅)会限制可以帮助您的人数。您可能想要添加 unix、ksh、bash。
There are numerous questions/answers on this topic posted here already on S.O.
已经在 SO 上发布了关于此主题的许多问题/答案
I hope this helps.
我希望这有帮助。
回答by Paul Rubel
The BashFAQnoted by shellter has some good examples. The basic idea, which I'm moving here so the page is self-contained, is to use an operation that both tests and sets at the same time: mkdir
该BashFAQ注意到shellter有一些很好的例子。我移到这里以便页面是自包含的基本思想是使用同时测试和设置的操作:mkdir
mkdir will fail if the directory exists and will make it if it does not. It's an atomic operation and you can use it like so to do a mutex in your shell script (from the above BashFAQ)
如果目录存在,mkdir 将失败,如果目录不存在,则创建它。这是一个原子操作,您可以像这样使用它在您的 shell 脚本中执行互斥锁(来自上面的 BashFAQ)
# Bourne
lockdir=/tmp/myscript.lock
if mkdir "$lockdir"
then # directory did not exist, but was created successfully
echo >&2 "successfully acquired lock: $lockdir"
# continue script
else # failed to create the directory, presumably because it already exists
echo >&2 "cannot acquire lock, giving up on $lockdir"
exit 0
fi
follow the link for more detail on cleanup and other items.
点击链接了解有关清理和其他项目的更多详细信息。
回答by PSkocik
You can use the flock
utility to lock a file / use it as a mutex.
您可以使用该flock
实用程序锁定文件/将其用作互斥锁。
Example:
例子:
#!/bin/sh -eu
#advanced bash stuff not needed
: >> lock #create a file if it doesn't exist
{
flock 3 #lock file by filedescriptor
echo $$ working with lock
sleep 2
echo $$ done with lock
} 3<lock
Example usage:
用法示例:
./mx & ./mx & ./mx & #will run one at a time cuz of the lock
(
(
In reply to massimo's point:
回复massimo的观点:
If you don't want to hardcode a filedecriptor number (it should rarely be a problem if you aren't hardcoding 0, 1, or 2, but anyway), then in bash (but not in a POSIX only shell) you can have the system pick a fd for you with:
如果您不想对文件描述符编号进行硬编码(如果您没有对 0、1 或 2 进行硬编码,那应该很少有问题,但无论如何),那么在 bash 中(但不是在仅 POSIX 的 shell 中),您可以拥有系统为您选择一个 fd:
{
flock $fd
#...
} {fd}<lock
)
)