如果您想每n分钟运行一次cron ,则取决于的值,有一些可能的选项n。
n 除以60(1、2、3、4、5、6、10、12、15、20、30)
在这里,通过使用/符号,解决方案很简单:
# 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
m-59/n * * * * command
在上面,n表示值n并且m表示小于n或的值*。这将在分钟执行命令m,m+n,m+2n,...
n 不除60
如果n不除以60,则无法使用cron干净地进行此操作,但有可能。为此,您需要在测试中检查时间的cron中进行测试。当查看UNIX时间戳(自以来的总秒数)时,最好这样做1970-01-01 00:00:00 UTC。假设我们要在Marty McFly到达Riverdale时第一次开始运行该命令,然后每隔一n分钟重复一次。
% date -d '2015-10-21 07:28:00' +%s
1445412480
为了使cronjob 42在`2015-10-21 07:28:00'之后的第nd分钟运行,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
* * * * * minutetestcmd "2015-10-21 07:28:00" 42 && command
与minutetestcmd定义为
#!/usr/bin/env bash
starttime=$(date -d "$1" "+%s")
# return UTC time
now=$(date "+%s")
# get the amount of minutes (using integer division to avoid lag)
minutes=$(( (now - starttime) / 60 ))
# set the modulo
modulo=$2
# do the test
(( now >= starttime )) && (( minutes % modulo == 0 ))
备注: UNIX时间不受leap秒影响
备注: cron没有亚秒精度