加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如果条件为true,请将cron作业休眠5分钟

发布时间:2020-12-13 18:14:05 所属栏目:Linux 来源:网络整理
导读:我有一个脚本设置在每分钟后运行.但是在我们提到的脚本中,如果条件为真,则脚本必须休眠5分钟.这怎么会影响crontab?脚本是否处于睡眠模式5分钟,或者它将再次运行,因为它在crontab中每隔1分钟设置一次? 解决方法 你有两个选择来获得这个.通常,cron与前一个作
我有一个脚本设置在每分钟后运行.但是在我们提到的脚本中,如果条件为真,则脚本必须休眠5分钟.这怎么会影响crontab?脚本是否处于睡眠模式5分钟,或者它将再次运行,因为它在crontab中每隔1分钟设置一次?

解决方法

你有两个选择来获得这个.通常,cron与前一个作业实例是否仍在运行无关.

选项1:

在脚本的开头写一个锁文件,并在完成后将其删除.然后在脚本开头检查文件是否存在,如果是,则脚本结束而不做任何事情.例如,这可能是这样的:

# if the file exists (`-e`) end the script:
[ -e "/var/lock/myscript.pid" ] && exit

# if not create the file:
touch /var/lock/myscript.pid

...
# do whatever the script does.
# if condition
sleep 300 # wait 5 min
...

# remove the file when the script finishes:
rm /var/lock/myscript.pid

选项2:

这也很有用.它被称为run-one.从联机帮助页:

run-one – run just one instance at a time of some command and unique
set of arguments (useful for cronjobs,eg)

然后cronjob可能如下所示:

* * * * *   /usr/bin/run-one /path/to/myscript

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读