英文:
Is there a command for cron job to run after every 14th of a month which should be a saturday(first Saturday after 14th or third Saturday of month)
问题
以下是翻译好的部分:
有没有一个cron作业命令,在每个月的14日后运行,该日应该是星期六(仅在第一个星期六或月份的第三个星期六之后),在晚上11点59分运行?
我需要知道cron作业的命令。
我已经使用了上面的表达式,但仍然不确定是否有其他更高效的表达式可以完成相同的工作。
英文:
Is there a command for the cron job to run after every 14th of a month which should be a Saturday(only on the first Saturday after 14th or the third Saturday of the month) at 11.59 pm
I need to know the command of the cron job
59 23 * * 6 [ "$(date +%d -d tomorrow)" -gt 14 ] && [ "$(date +%u -d tomorrow)" -eq 6 ] && the task
I have used the above expression. but still unsure if any other expression can do the same job more efficiently.
答案1
得分: 1
更新: @jhnc 发现规范 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html) 指出,尽管有很多“ANDing”,但有时也有“ORing”:“最后,如果月份或月份的某一天被指定为元素或列表,并且星期几也被指定为元素或列表,则将匹配与月份和月份相匹配的任何日期,或星期几相匹配的日期。”
基于此,我认为您最初的方法可能是正确的。
原始回答:
cron
字段被“and”在一起,并且允许范围,因此这应该可以工作:
59 23 14-31 * 6 <任务>
英文:
UPDATE: @jhnc found that the spec (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html) indicates that while there is a lot of "ANDing", sometimes there is "ORing": "Finally, if either the month or day of month is specified as an element or list, and the day of week is also specified as an element or list, then any day matching either the month and day of month, or the day of week, shall be matched."
Based on that, I think your original approach is probably the right one.
ORIGINAL ANSWER:
cron
fields are "anded" together, and ranges are permitted, so this should work:
59 23 14-31 * 6 <the task>
答案2
得分: 0
以下是翻译好的部分:
"The first Saturday after the 14th is somewhere in the range 15 to 21:"(在第14号之后的第一个星期六在15到21号之间。)
"The hyphen in %-d
is intended to remove the zero-padding from the day so 08
and 09
are not interpreted as invalid octal numbers. Check your date
man page."(%-d
中的连字符旨在去掉日期中的零填充,以确保08
和09
不会被解释为无效的八进制数字。请查看您的date
手册页面。)
"I'm confused why you're launching the cron entry at 23:59 and checking tomorrow, so I launched at 00:01 and checked today."(我不明白为什么您要在23:59启动cron任务条目然后检查明天,所以我在00:01启动并在今天进行了检查。)
"The cron entry is already constrained by day 6, so I'm not double checking that with date."(cron任务条目已经限定在星期六,所以我不会再用日期来双重检查。)
英文:
The first Saturday after the 14th is somewhere in the range 15 to 21:
01 00 * * 6 d=$(date '+%-d'); [ "$d" -ge 15 ] && [ "$d" -le 21 ] && the task
The hyphen in %-d
is intended to remove the zero-padding from the day so 08
and 09
are not interpreted as invalid octal numbers. Check your date
man page.
I'm confused why you're launching the cron entry at 23:59 and checking tomorrow, so I launched at 00:01 and checked today.
The cron entry is already constrained by day 6, so I'm not double checking that with date.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论