shell脚本:确保如果已经运行不执行该脚本 [英] Shell script: Ensure that script isn't executed if already running

查看:264
本文介绍了shell脚本:确保如果已经运行不执行该脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at\">Quick-and-dirty这样才能保证一个shell脚本只有一个实例是在同一时间运行

我已经建立了一个cronjob备份我的文件夹正确,我感到很自豪。不过我发现了,通过观察从备份的结果,我的备份脚本已经被称为crontab的不止一次,导致在同时运行多个备份。

I've set up a cronjob to backup my folders properly which I am quite proud of. However I've found out, by looking at the results from the backups, that my backup script has been called more than once by Crontab, resulting in multiple backups running at the same time.

有什么办法,我可以保证,如果同样的脚本已经在执行一定的shell脚本不能运行?

Is there any way I can ensure that a certain shell script to not run if the very same script already is executing?

感谢

推荐答案

不分种族条件或提前退出问题的一个解决方案是使用一个锁文件。在羊群程序处理这非常好,可以这样使用的:

A solution without race condition or early exit problems is to use a lock file. The flock utility handles this very well and can be used like this:

flock -n /var/run/your.lockfile -c /your/script

它会立即用非0的状态,如果脚本已经运行恢复。

It will return immediately with a non 0 status if the script is already running.

这篇关于shell脚本:确保如果已经运行不执行该脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆