每个月的第二个和第四个星期六运行 [英] Run every 2nd and 4th Saturday of the month

查看:23
本文介绍了每个月的第二个和第四个星期六运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每月的第 2 个和第 4 个星期六运行作业的 cron (linux) 语法是什么?

What's the cron (linux) syntax to have a job run on the 2nd and 4th Saturday of the month?

推荐答案

每月的第二个星期六是从 8th 到 14th 包括在内.同样,第四个星期六是在 22nd 和 28th 之间的一个日期.

The second Saturday of the month falls on one (and only one) of the dates from the 8th to the 14th inclusive. Likewise, the fourth Saturday falls on one date between the 22nd and the 28th inclusive.

您可能认为可以使用星期字段将其限制为星期六(下面一行中的 6):

You may think that you could use the day of week field to limit it to Saturdays (the 6 in the line below):

0 1 8-14,22-28 * 6 /path/to/myscript

不幸的是,根据联机帮助页,月中的某天和一周中的某天是一个 OR 命题,其中任一个都会导致作业运行:

Unfortunately, the day-of-month and day-of-week is an OR proposition where either will cause the job to run, as per the manpage:

命令由 cron 执行,当分钟、小时和月份字段与当前时间匹配,并且当至少两个天字段(日月份或星期几)匹配当前时间.

Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) match the current time.

命令执行的日期可以由两个字段指定 - 月中的某天和一周中的某天.如果两个字段都受到限制(即不是 *),则当任一字段与当前时间匹配时,将运行该命令.例如,30 4 1,15 * 5 将使命令在每个月的 1 号和 15 号以及每个星期五的凌晨 4:30 运行.

The day of a command's execution can be specified by two fields - day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, 30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

因此,您需要将 cron 作业设置为在每一天运行,如果不是星期六,则立即退出.

Hence you need to set up your cron job to run on every one of those days and immediately exit if it's not Saturday.

cron 条目是这样的:

0 1 8-14,22-28 * * /path/to/myscript

(每天凌晨 1 点运行).

(to run at 1am on each possible day).

然后,在/path/to/myscript的顶部,放:

# Exit unless Saturday
if [[ $(date +%u) -ne 6 ]] ; then
    exit
fi

而且,如果您不能修改脚本(例如,如果它是一个程序),只需编写一个仅包含该检查和对该程序调用的脚本,然后从 cron 代替.

And, if you can't modify the script (e.g., if it's a program), simply write a script containing only that check and a call to that program, and run that from cron instead.

您也可以将星期六的测试放入 crontab 文件本身,以将所有调度数据保存在一个地方:

You can also put the test for Saturday into the crontab file itself to keep the scheduling data all in one place:

0 1 8-14,22-28 * * [ `date +\%u` = 6 ] && /path/to/myscript

这篇关于每个月的第二个和第四个星期六运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆