crontab (cronie), 运行 一个月中的某些日子

crontab (cronie), run certain days of the month

我想创建一个 crontab 条目,它将 运行 每个工作日,除了每个月的 1 号、2 号和 3 号。这是我拥有的:

45   8,12,16 4-31 * 1-5  my_program

我认为这具有以下含义:

minutes:      45 past the hour
hours:        8, 12, and 16
day of month: 4th to 31st inclusive
month:        all
day of week:  Monday to Friday inclusive

这是我在 crontab(5) 手册页中读到的内容。但是,我可以在今天(10 月 2 日星期一)08:45 看到作业 运行。我确定 crontab 文件已正确加载(并且 crontab -l 显示了它);它最近没有改变。 date显示的系统日期和时间也是正确的。

我缺少什么来确保我的 cron 作业从 4 号开始只有 运行s?我正在使用 Fedora Linux:

% rpm -qf `which crontab`
cronie-1.5.0-3.fc23.x86_64
% rpm -q fedora-release
fedora-release-23-1.noarch

来自 crontab 手册:

Note: 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. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

提到的示例如下所示:

0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"

所以在你的情况下它会像这样(未测试):

45   8,12,16 4-31 * *  test $(date +\%u) -lt 6 && my_program