如何避免shell脚本被同时运行多次
http://astellar.com/2012/10/backups-running-at-the-same-time/
http://www.davidpashley.com/articles/writing-robust-shell-scripts.html
比如说有一个周期性(cron)备份mysql的脚本,或者rsync脚本,
如果出现意外,运行时间过长,
很有可能下一个备份周期已经开始了,当前周期的脚本却还没有运行完,
显然我们都不愿意看到这样的情况发生。
其实只要对脚本自身做一些改动,就可以避免它被重复运行。
#!/bin/bash LOCK_NAME="/tmp/my.lock" if [[ -e $LOCK_NAME ]] ; then echo "re-entry, exiting" exit 1 fi ### Placing lock file touch $LOCK_NAME echo -n "Started..." ### 开始正常流程 ### 正常流程结束 ### Removing lock rm -f $LOCK_NAME echo "Done." |
当脚本开始运行时, …
[获取更多]