下面的锁机制用于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 1flock -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的羊群:如果出口不能获得锁
发布评论