如何设置cron作业每5天运行一次?

时间:2020-12-10 01:44:07

I want to setup cronjob which will start on for example today and it will run every 5 days. This is what I have now, is this will work correctly ? If I install this job at 5 o`clock and then every 5 days on 6 AM.

我想设置cronjob,它将在今天开始,它将每5天运行一次。这就是我现在所拥有的,这是否会正常工作?如果我在5点钟安装这个工作,然后在早上6点安装这个工作。

0 6 */5 * * mailx -r root@mail.com -s "Message title" -c "cc@mail.com" primary@mail.com < body.txt

0 6 * / 5 * * mailx -r root@mail.com -s“消息标题”-c“cc@mail.com”primary@mail.com

2 个解决方案

#1


0 0 */5 * *  midnight every 5 days.

See this, similar question just asked for every 3 days. Cron job every three days

看到这个,每3天就提出一个类似的问题。 Cron每三天工作一次

#2


Bear with me, this is my first time posting an answer... let me know if anything I put is unclear.

忍受我,这是我第一次发布答案...如果我提出的任何内容都不清楚,请告诉我。

I don't think you have what you need with:

我不认为你有你需要的东西:

0 0 */5 * * ## <<< WARNING!!! CAUSES UNEVEN INTERVALS AT END OF MONTH!!

Unfortunately, the */5 is setting the interval based on day of the month. See: explanation here. At the end of the month there is recurring issue guaranteed.

不幸的是,* / 5是根据每月的日期设置间隔。见:这里的解释。在月底,保证重复出现问题。

1st  at 2019-01-01 00:00:00
then at 2019-01-06 00:00:00 << 5 days, etc. OK
then at 2019-01-11 00:00:00
...
then at 2019-01-26 00:00:00
then at 2019-01-31 00:00:00
then at 2019-02-01 00:00:00 << 1 day WRONG
then at 2019-02-06 00:00:00
...
then at 2019-02-26 00:00:00
then at 2019-03-01 00:00:00 << 3 days WRONG

According to this article, you need to add some modulo math to the command being executed to get a TRUE "every N days". For example:

根据这篇文章,你需要为正在执行的命令添加一些模数学,以“每N天”获得一个TRUE。例如:

0 0 * * *  bash -c '(( $(date +\%s) / 86400 \% 5 == 0 )) && runmyjob.sh

In this example, the job will be checked daily at 12:00 AM, but will only execute when the number of days since 01-01-1970 modulo 5 is 0.

在此示例中,将每天在凌晨12:00检查作业,但仅在自01-01-1970模5的天数为0时执行。

If you want it to be every 5 days from a specific date, use the following format:

如果您希望从特定日期开始每隔5天,请使用以下格式:

0 0 * * *  bash -c '(( $(date +\%s -d "2019-01-01") / 86400 \% 5 == 0 )) && runmyjob.sh

#1


0 0 */5 * *  midnight every 5 days.

See this, similar question just asked for every 3 days. Cron job every three days

看到这个,每3天就提出一个类似的问题。 Cron每三天工作一次

#2


Bear with me, this is my first time posting an answer... let me know if anything I put is unclear.

忍受我,这是我第一次发布答案...如果我提出的任何内容都不清楚,请告诉我。

I don't think you have what you need with:

我不认为你有你需要的东西:

0 0 */5 * * ## <<< WARNING!!! CAUSES UNEVEN INTERVALS AT END OF MONTH!!

Unfortunately, the */5 is setting the interval based on day of the month. See: explanation here. At the end of the month there is recurring issue guaranteed.

不幸的是,* / 5是根据每月的日期设置间隔。见:这里的解释。在月底,保证重复出现问题。

1st  at 2019-01-01 00:00:00
then at 2019-01-06 00:00:00 << 5 days, etc. OK
then at 2019-01-11 00:00:00
...
then at 2019-01-26 00:00:00
then at 2019-01-31 00:00:00
then at 2019-02-01 00:00:00 << 1 day WRONG
then at 2019-02-06 00:00:00
...
then at 2019-02-26 00:00:00
then at 2019-03-01 00:00:00 << 3 days WRONG

According to this article, you need to add some modulo math to the command being executed to get a TRUE "every N days". For example:

根据这篇文章,你需要为正在执行的命令添加一些模数学,以“每N天”获得一个TRUE。例如:

0 0 * * *  bash -c '(( $(date +\%s) / 86400 \% 5 == 0 )) && runmyjob.sh

In this example, the job will be checked daily at 12:00 AM, but will only execute when the number of days since 01-01-1970 modulo 5 is 0.

在此示例中,将每天在凌晨12:00检查作业,但仅在自01-01-1970模5的天数为0时执行。

If you want it to be every 5 days from a specific date, use the following format:

如果您希望从特定日期开始每隔5天,请使用以下格式:

0 0 * * *  bash -c '(( $(date +\%s -d "2019-01-01") / 86400 \% 5 == 0 )) && runmyjob.sh