我有一个脚本设置后,每分钟运行。但是在我们提到的脚本中,如果条件为真,则脚本必须休眠5分钟。这怎么会影响crontab呢?该脚本是处于睡眠模式5分钟,还是它将再次运行,因为它在crontab设置的每1分钟?
发布于 2015-01-21 07:35:11
你有两个选择来获得这个。通常,cron并不在乎以前的作业实例是否仍在运行。
在脚本开始时编写一个锁文件,并在它完成后删除它。然后,在脚本开始时进行检查,查看文件是否存在,如果存在,脚本就会结束,而不会执行某些操作。例如,它可能如下所示:
# 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这也是有用处的。它叫run-one。在这篇文章中:
在某个命令和唯一的参数集中,一次只运行一个实例(例如,对cron作业有用)。
然后,cronjob看起来可能是这样的:
* * * * * /usr/bin/run-one /path/to/myscripthttps://serverfault.com/questions/661139
复制相似问题