gpt4 book ai didi

linux - 如何在每个月的第一个星期五运行 cronjob?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:38 25 4
gpt4 key购买 nike

我每个月的第一个星期五晚上都有 cronjob我使用了下面提到的条目

00 20 1-7  * Fri         [ "$(date '+\%a')" = "Fri" ] && $HOME/path/to/my/script.sh > /dev/null 2>&1

如果星期五在一个月的 1-7 日内,此条目应该运行我的脚本,但我的脚本甚至在 7 日之后(即在该月的所有星期五)执行。请建议如何修复它。

最佳答案

这是因为当您指定月中的某一天和星期几时,cron 将在这些约束中的任何一个为真时执行作业。来自 crontab (5) 的手册页:

   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 cur‐
rent 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 中没有直接的方法来做你想做的事,但是 cron : how to schedule to run first Sunday of every month描述了使用 cron 运行脚本的解决方法,例如每个星期五,然后在脚本中计算日期是否在 1-7 范围内,只有在这种情况下才继续。

针对关于使用 5 而不是 Fri 来指定星期几的评论:使用 Fri 是可以的,如手册页所述:

   Months or days of the week can be specified by name.

关于linux - 如何在每个月的第一个星期五运行 cronjob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24217135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com