Crontab 一周内某些日子发生冲突

Crontab 一周内某些日子发生冲突

我在 centos 6 crontab 上设置了备份轮换,一个脚本应该在周一至周六运行,另一个在周日运行,从不在每月 1 号运行。但是每周(仅周日)脚本每晚都在运行,每日脚本在周日运行。cron.log 已确认

#Daily - Midnight, Mon-Sat, Not on the 1st of the Month
0 0 2-31 * 1-6 script_daily

#Weekly - Midnight, Sunday, Not on the 1st of the Month
0 0 2-31 * 0 script_weekly

我不知道我做错了什么?

编辑:我从 cron 日志中提取了相关行。28 日是星期日。

Jun 28 00:00:01 backup CROND[2000]: (root) CMD (script_daily)
Jun 28 00:00:01 backup CROND[2004]: (root) CMD (script_weekly)
Jun 29 00:00:01 backup CROND[9438]: (root) CMD (script_daily)
Jun 29 00:00:01 backup CROND[9443]: (root) CMD (script_weekly)
Jun 30 00:00:02 backup CROND[6893]: (root) CMD (script_daily)
Jun 30 00:00:02 backup CROND[6898]: (root) CMD (script_weekly)

答案1

手册页

If you  spec-
ify  both  a day in the month and a day of week, it will be interpreted
as the Nth such day in the month.

[...]

To request the last Monday, etc.  in a month, ask for  the  "5th"  one.
This  will  always  match the last Monday, etc., even if there are only
four Mondays in the month:


     # run at 11 am on the first and last Mon, Tue, Wed of each month
     0 11 1,5 * mon-wed date

When the fourth Monday in a month is the last, it  will  match  against
both  the "4th" and the "5th" (it will only run once if both are speci-
fied).

简而言之...

0 0 2-31 * 1-6 script_daily

每月第二个至最后一个星期一/星期二/星期三/星期四/星期五/星期六

0 0 2-31 * 0 script_weekly

每月第二个星期日至最后一个星期日

我首先想到的答案是“教你的脚本在每月 1 号运行时立即退出”,然后按如下方式运行你的作业

0 0 * * 1-6 script_daily
0 0 * * 0 script_weekly

相关内容