如何确保一次只有一个 Bash 脚本实例在运行?
我想制作一个在任何时候最多只运行一次的 sh 脚本。
比如说,如果我执行脚本,然后我再次执行脚本,我该如何做到这一点,以便如果脚本的第一个执行仍在工作,第二个执行将失败并出现错误。即我需要在执行任何操作之前检查脚本是否在其他地方运行。我该怎么做呢?
我的脚本运行一个长时间运行的进程(即永远运行)。我想使用 cron 之类的东西每 15 分钟调用一次脚本,这样万一进程失败,它将由下一个 cron 运行脚本重新启动。
I want to make a sh script that will only run at most once at any point.
Say, if I exec the script then I go to exec the script again, how do I make it so that if the first exec of the script is still working the second one will fail with an error. I.e. I need to check if the script is running elsewhere before doing anything. How would I go about doing this??
The script I have runs a long running process (i.e. runs forever). I wanted to use something like cron to call the script every 15mins so in case the process fails, it will be restarted by the next cron run script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你想要一个 pid 文件,也许是这样的:
You want a pid file, maybe something like this:
我认为你需要使用lockfile命令。请参阅在 shell 脚本 (BASH) 中使用锁定文件 或 http://www.davidpashley.com/articles/writing-robust-shell-脚本.html。
第二篇文章使用“手工制作的锁定文件”并展示如何捕获脚本终止和终止。释放锁;尽管在大多数情况下使用lockfile -l <timeout秒>可能是一个足够好的替代方案。
无超时的使用示例:
将确保在此期间启动的任何第二个脚本将无限期地等待文件被删除,然后再继续。
如果我们知道脚本运行时间不应超过 X 秒,并且 script.lock 仍然存在,这可能意味着脚本的前一个实例在删除 script.lock 之前已被终止/代码>。在这种情况下,我们可以告诉
lockfile
在超时后强制重新创建锁(下面的 X = 10):由于
lockfile
可以创建多个锁文件,因此有一个参数指导它在重试获取所需的下一个文件之前应该等待多长时间(-<重试前睡眠,秒数>
和-r <重试次数>
)。还有一个参数-s <挂起秒数>
用于强制解除锁定时的等待时间(这补充了强制破坏锁定之前用于等待的超时时间)。I think you need to use lockfile command. See using lockfiles in shell scripts (BASH) or http://www.davidpashley.com/articles/writing-robust-shell-scripts.html.
The second article uses "hand-made lock file" and shows how to catch script termination & releasing the lock; although using
lockfile -l <timeout seconds>
will probably be a good enough alternative for most cases.Example of usage without timeout:
Will ensure that any second script started during this one will wait indefinitely for the file to be removed before proceeding.
If we know that the script should not run more than X seconds, and the
script.lock
is still there, that probably means previous instance of the script was killed before it removedscript.lock
. In that case we can telllockfile
to force re-create the lock after a timeout (X = 10 below):Since
lockfile
can create multiple lock files, there is a parameter to guide it how long it should wait before retrying to acquire the next file it needs (-<sleep before retry, seconds>
and-r <number of retries>
). There is also a parameter-s <suspend seconds>
for wait time when the lock has been removed by force (which kind of complements the timeout used to wait before force-breaking the lock).您可以使用
run-one
包,它提供run-one
、run-this-one
和keep-one-running
。软件包:https://launchpad.net/ubuntu/+source/run-one
介绍它的博客:http://blog.dustinkirkland.com/2011/02/introducing-run-one-and-run-this-one.html
You can use the
run-one
package, which providesrun-one
,run-this-one
andkeep-one-running
.The package: https://launchpad.net/ubuntu/+source/run-one
The blog introducing it: http://blog.dustinkirkland.com/2011/02/introducing-run-one-and-run-this-one.html
将进程 ID 写入文件,然后当新实例启动时,检查该文件以查看旧实例是否仍在运行。
Write the process id into a file and then when a new instance starts, check the file to see if the old instance is still running.
来自集群(1)手册页中的示例。在 shell 脚本中使用非常实用。
From example in flock(1) man page. Very practical for using in shell scripts.
我刚刚写了一个工具来执行此操作:
https://github.com/ORESoftware/quicklock
写一篇好的文章大约需要 15 loc,所以不是您想要包含在每个 shell 脚本中的内容。
基本上是这样工作的:
上面调用了这个bash函数:
当脚本退出时,它会使用trap自动释放锁
,随意手动释放锁,使用
ql_release_lock
:I just wrote a tool that does this:
https://github.com/ORESoftware/quicklock
writing a good one takes about 15 loc, so not something you want to include in every shell script.
basically works like this:
the above calls this bash function:
when the script exits, it automatically releases the lock using trap
to manually release the lock at will, use
ql_release_lock
:我建议使用
flock
,但方式与@Josef Kufner 建议的方式不同。我认为这非常简单,默认情况下flock
应该在大多数系统上可用:I suggest using
flock
, but in a different way than suggested by @Josef Kufner. I think this is quite easy andflock
should be available on most systems by default: