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

我有一个脚本设置在每分钟后运行.但是在我们提到的脚本中,如果条件为真,则脚本必须休眠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

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...