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

时间:2021-11-04 04:01:54

I have cronjob to be run on every month first friday evening i used the below mentioned entry

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

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

This entry should run my script if Friday falls withing 1-7 day of the month, but my script is getting executed even after 7th (i.e on all Fridays of the month). Please suggest how to fix it.

如果星期五在每月的1-7天下降,那么这个条目应该运行我的脚本,但是我的脚本即使在7日之后也会被执行(即在该月的所有星期五)。请建议如何解决它。

1 个解决方案

#1


1  

This is because when you specify a day of month and day of week, cron will execute the job when EITHER of those constraints are true. From the man page for crontab (5):

这是因为当您指定月中的某一天和星期几时,当这些约束条件为真时,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.

There isn't a direct way in cron to do what you want, but cron : how to schedule to run first Sunday of every month describes a workaround by using cron to run your script e.g. every Friday and then calculating in the script if the day of month is in the range 1-7, and only continuing when that is the case.

在cron中没有直接的方法可以做你想做的事情,但是cron:如何安排每个月的第一个星期日运行,通过使用cron运行你的脚本描述了一种解决方法,例如:每个星期五,然后如果月中的日期在1-7范围内,则在脚本中计算,并且只有在这种情况下才继续。

In response to the comment about using 5 rather than Fri to specify day of week: using Fri is OK, as the man page says:

回应关于使用5而不是Fri来指定星期几的评论:使用Fri是可以的,因为手册页说:

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

#1


1  

This is because when you specify a day of month and day of week, cron will execute the job when EITHER of those constraints are true. From the man page for crontab (5):

这是因为当您指定月中的某一天和星期几时,当这些约束条件为真时,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.

There isn't a direct way in cron to do what you want, but cron : how to schedule to run first Sunday of every month describes a workaround by using cron to run your script e.g. every Friday and then calculating in the script if the day of month is in the range 1-7, and only continuing when that is the case.

在cron中没有直接的方法可以做你想做的事情,但是cron:如何安排每个月的第一个星期日运行,通过使用cron运行你的脚本描述了一种解决方法,例如:每个星期五,然后如果月中的日期在1-7范围内,则在脚本中计算,并且只有在这种情况下才继续。

In response to the comment about using 5 rather than Fri to specify day of week: using Fri is OK, as the man page says:

回应关于使用5而不是Fri来指定星期几的评论:使用Fri是可以的,因为手册页说:

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