我一直在寻找一个每50个小时执行一次脚本的cron计划。使用cron,每2天运行一个工作很简单。我们可以代表
0 0 */2 * * command
但是每50个小时呢?
答案 0 :(得分:3)
如果你想每n
小时运行一个cron,n
不分24,你就不能用cron干净地完成这个,但是有可能。为此,您需要在测试检查时间的cron中进行测试。在查看UNIX时间戳(自1970-01-01 00:00:00 UTC
以来的总秒数)时,最好这样做。让我们说我们想要从McFly抵达Riverdale的那一刻开始:
% date -d '2015-10-21 07:28:00' +%s
1445412480
对于在2015-10-21 07:28:00'之后50
小时运行的cronjob,crontab将如下所示:
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
28 * * * * hourtestcmd "2015-10-21 07:28:00" 50 && command
将hourtestcmd
定义为
#!/usr/bin/env bash
starttime=$(date -d "$1" "+%s")
# return UTC time
now=$(date "+%s")
# get the amount of hours
hours=$(( (now - starttime) / 3600 ))
# get the amount of remaining minutes
minutes=$(( (now - starttime - hours*3600) / 60 ))
# set the modulo
modulo=$2
# do the test
(( now >= starttime )) && (( hours % modulo == 0 )) && (( minutes == 0 ))
备注: UNIX时间以UTC格式给出。如果您的cron运行在受夏令时影响的不同时区,则可能导致程序以偏移量执行,或者夏令时变为活动时,增量为51小时或49小时。
备注: UNIX时间不受闰秒的影响
备注: cron
没有亚秒级准确度
备注:请注意我如何将分钟与开始时间中的分钟相同。这样可以确保cron每小时运行一次。