问题描述
我需要停止 cron 作业的重叠,例如:如果一个 cron 作业安排在早上 2 点钟进行数据库备份,而其他 cron 作业安排在早上 7 点钟进行数据库备份.所以我需要停止如果 2 点钟的 DB 备份未完成,则 7 点钟计划的 cron 作业.
I need to stop the overlapping of cron jobs for example:If a cron job is scheduled at morning 2 o clock for DB backup and other cron job is scheduled at morning 7 o clock for DB backup again.So i need to stop the 7 o clock scheduled cron job if the DB backup for 2 o clock is not completed.
推荐答案
这就是 flock 的用途.来自man flock
:
This is what flock is for. From man flock
:
...
The third form is convenient inside shell scripts, and is usually used
the following manner:
(
flock -n 9 || exit 1
# ... commands executed under lock ...
) 9>/var/lock/mylockfile
...
在这种情况下,flock 将尝试为 /var/lock/myfile
获取 fd 9 上的锁定.如果不能(因为之前的作业仍在运行),它将退出.
In this case, flock will try to get a lock on fd 9 for /var/lock/myfile
. If it can't (because the earlier job is still running), it will exit.
您需要将其放入脚本中.
You need to put it in a script.
这篇关于停止 Cron 作业重叠的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!