Cron 每25小时做一次工作?

Cron 每25小时做一次工作?,cron,Cron,如何设置每25小时运行一次的cronjob?只是猜测,但您不知道 最好的办法是:写一个脚本来跟踪它上次运行的时间,如果它是在25个小时前运行的话,就有条件地运行它 Cron使驱动程序脚本每小时运行一次。在启动当前作业时,发出指定下一个作业的时间和日期的命令会更容易,但您可以通过在当前运行开始时更新进程的cronjob条目来模拟cronjob(不是在最后,因为你必须考虑运行作业的时间)。你可以使用“sleep”或“watch”命令让脚本在循环中运行。只需确保脚本已执行。设置每小时一次的作业,如果使

如何设置每25小时运行一次的cronjob?

只是猜测,但您不知道

最好的办法是:写一个脚本来跟踪它上次运行的时间,如果它是在25个小时前运行的话,就有条件地运行它


Cron使驱动程序脚本每小时运行一次。

在启动当前作业时,发出指定下一个作业的时间和日期的命令会更容易,但您可以通过在当前运行开始时更新进程的cronjob条目来模拟cronjob(不是在最后,因为你必须考虑运行作业的时间)。

你可以使用“sleep”或“watch”命令让脚本在循环中运行。只需确保脚本已执行。

设置每小时一次的作业,如果使用此snipnet已过25小时,请检查脚本:

if [ $((((`date +%s` - (`date +%s` % 3600))/3600) % 25)) -eq 0 ] ; then
 your script 
fi

我想你应该试试这个

0 */25 * * * ...

如果从开始计算小时(、分钟、天或周),在脚本顶部添加一个条件,并将脚本设置为在crontab上每小时运行一次,则可以实现任意频率:

#!/bin/bash

hoursSinceEpoch=$(($(date +'%s / 60 / 60')))

# every 25 hours
if [[ $(($hoursSinceEpoch % 25)) -ne 0 ]]; then
    exit 0
fi
返回当前日期,我们将其格式化为自历元起的秒(
%s
),然后我们进行基本数学运算:

# .---------------------- bash command substitution
# |.--------------------- bash arithmetic expansion
# || .------------------- bash command substitution
# || |  .---------------- date command
# || |  |   .------------ FORMAT argument
# || |  |   |      .----- formula to calculate minutes/hours/days/etc is included into the format string passed to date command
# || |  |   |      |
# ** *  *   *      * 
  $(($(date +'%s / 60')))
# * *  ---------------
# | |        | 
# | |        ·----------- date should result in something like "1438390397 / 60"
# | ·-------------------- it gets evaluated as an expression. (the maths)
# ·---------------------- and we can store it
您可以将此方法用于每分钟、每小时、每天或每月的cron作业:

#!/bin/bash
# We can get the

minutes=$(($(date +'%s / 60')))
hours=$(($(date +'%s / 60 / 60')))
days=$(($(date +'%s / 60 / 60 / 24')))
weeks=$(($(date +'%s / 60 / 60 / 24 / 7')))

# or even

moons=$(($(date +'%s / 60 / 60 / 24 / 656')))

# passed since Epoch and define a frequency
# let's say, every 13 days

if [[ $(($days % 13)) -ne 0 ]]; then
    exit 0
fi

# and your actual script starts here

这样做的目的是什么?为什么24小时不被接受?另请参见:来自谷歌的一个合法用例,它是为了避免对时间不敏感的定期作业超过每日API限制。增加一个额外的小时以保持保守并避免夏令时错误。如果你错过一个作业,例如,如果系统在计划运行时关闭。请多加评论。此代码生成时间戳(自历元起的秒数),将其转换为小时,并检查模数25(因此每25小时)。如果需要,请将“-eq 0”更改为“-eq 10”以更改特定日期的小时数…您不能将小时频率设置为23以上