bash Shell 脚本:如果脚本已经在运行,请确保不执行该脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4768860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:05:57  来源:igfitidea点击:

Shell script: Ensure that script isn't executed if already running

bashshellcron

提问by Industrial

Possible Duplicate:
Quick-and-dirty way to ensure only one instance of a shell script is running at a time

可能的重复:
确保一次只运行一个 shell 脚本实例的快速而肮脏的方法

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.

我已经设置了一个 cronjob 来正确备份我的文件夹,我为此感到非常自豪。但是,通过查看备份结果,我发现我的备份脚本已被 Crontab 多次调用,导致同时运行多个备份。

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

如果相同的脚本已经在执行,有什么方法可以确保某个 shell 脚本不会运行?

回答by Arnaud Le Blanc

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

没有竞争条件或提前退出问题的解决方案是使用锁定文件。该flock实用程序可以很好地处理这个问题,可以像这样使用:

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

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

如果脚本已经在运行,它将立即以非 0 状态返回。

回答by paxdiablo

The usual and simple way to do this is to put something like:

执行此操作的通常且简单的方法是输入以下内容:

if [[ -f /tmp/myscript.running ]] ; then
    exit
fi
touch /tmp/myscript.running

at the top of you script and

在你的脚本顶部和

rm -f /tmp/myscript.running

at the end, and in trapfunctions in case it doesn't reach the end.

最后,在trap函数中,以防它没有到达终点。

This still has a few potential problems (such as a race condition at the top) but will do for the vast majority of cases.

这仍然有一些潜在的问题(例如顶部的竞争条件),但对于绝大多数情况都可以。

回答by David Hall

A good way without a lock file:

没有锁文件的好方法:

ps | grep ##代码## | grep -v grep > /var/tmp/##代码##.pid
pids=$(cat /var/tmp/##代码##.pid | cut -d ' ' -f 1)
for pid in $pids
do
   if [ $pid -ne $$ ]; then
      logprint " ##代码## is already running. Exiting"
      exit 7
   fi
done
rm -f /var/tmp/##代码##.pid

This does it without a lock file, which is cool. ps into a temp file, scrape the first field (pid #) and look for ourselves. If we find a different one, then somebody's already running. The grep $0 is to shorten the list to just those instances of this program, and the grep -v grep gets rid of the line that is the grep itself:)

这样做没有锁定文件,这很酷。ps 进入一个临时文件,抓取第一个字段(pid #)并查找我们自己。如果我们找到一个不同的,那么已经有人在跑了。grep $0 是将列表缩短为该程序的那些实例,而 grep -v grep 去掉了 grep 本身的行:)

回答by elp

You can use a tmp file.
Named it tmpCronBkp.a, tmpCronBkp.b, tmpCronBkp.c... etc. Releated of yout backup script.

您可以使用 tmp 文件。
将其命名为 tmpCronBkp.a、tmpCronBkp.b、tmpCronBkp.c...等。与您的备份脚本相关。

Create it on script start and delete it at the end...

在脚本开始时创建它并在最后删除它...

In a while, check if file exist or not and what file exist.

过一会儿,检查文件是否存在以及存在什么文件。

Have you tried this way?

你试过这种方法吗?