bash的羊群:如果出口不能获得锁 [英] bash flock: exit if can't acquire lock
问题描述
下面的锁机制用于preventing从并行运行的cron
工作
#!/斌/庆典回声临界区之前
(
涌向-e 200
回声在关键节
睡眠5
)200 GT; /tmp/blah.lockfile
回声临界区后
当运行两个实例togeather,后来的等待,直到第一饰面,然后运行。这可能会导致脚本等待运行的积压。
我如何改变这个脚本,这样,如果羊群
无法获取锁,它终止脚本?我试过 -n
没有成功。
羊群-n -e 200 || 1号出口
羊群-n
告诉你它未能通过返回失败code(零以外的东西)。你可以做,而不是在你的脚本,使当它看到任何错误选中它退出的顶部设置-e
。
根据您的应用程序时,您可能希望 0退出
来表示成功时,不能获取锁。
The following lock mechanism is used for preventing a cron
job from running concurrently:
#!/bin/bash
echo "Before critical section"
(
flock -e 200
echo "In critical section"
sleep 5
) 200>/tmp/blah.lockfile
echo "After critical section"
When running two instances togeather, the later waits until the first finishes, and then runs. This can cause backlogs of scripts waiting to run.
How do I alter this script so that if flock
can't acquire the lock, it terminates the script? I've tried -n
without success.
flock -n -e 200 || exit 1
flock -n
tells you it failed by returning a failure code (something other than zero). You could instead do set -e
at the top of your script to make it exit when it sees any unchecked error.
Depending on your application, you might want to exit 0
to indicate success when the lock can't be acquired.
这篇关于bash的羊群:如果出口不能获得锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!