我怎样才能让cron每隔一个半小时运行一些命令呢?
我怎样才能让cron每隔一个半小时运行一些命令呢?
在正常的cron
这是不可能的。
不用修改代码就可以做的最好的是:
0 0,3,6,9,12,15,18,21 * * * [cmd]
30 1,4,7,10,13,16,19,22 * * * [cmd]
这些可能是可压缩的,取决于你必须的cron版本:
0 */3 * * * [cmd]
30 1-23/3 * * * [cmd]
为什么不能使用1小时或2小时有充分的理由吗? 肯定会更简单一些。
我没有亲自尝试过,但是你可以在这里find一些关于让cron每90分钟运行一次的信息: http : //keithdevens.com/weblog/archive/2004/May/05/cron
从上面的链接excert:
0 0,3,6,9,12,15,18,21 * * * <commands> 30 1,4,7,10,13,16,19,22 * * * <commands>
在crontab中有两行。 沿着:
0 0,3,6,9,12,15,18,21 * * * /usr/bin/foo 30 1,4,7,10,13,16,19,22 * * * /usr/bin/foo
你可以用两个crontab条目来完成。 每三个小时运行一次,它们被90分钟抵消,如下所示:
0 0,3,6,9,12,15,18,21 * * *
30 1,4,7,10,13,16,19,22 * * *
#! /bin/sh # Minute Cron # Usage: cron-min start # Copyright 2014 by Marc Perkel # docs at http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every_few_seconds_under_cron" # Free to use with attribution # Run this script under Cron once a minute basedir=/etc/cron-min if [ $# -gt 0 ] then echo echo "cron-min by Marc Perkel" echo echo "This program is used to run all programs in a directory in parallel every X minutes." echo echo "Usage: cron-min" echo echo "The scheduling is done by creating directories with the number of minutes as part of the" echo "directory name. The minutes do not have to evenly divide into 60 or be less than 60." echo echo "Examples:" echo " /etc/cron-min/1 # Executes everything in that directory every 1 minute" echo " /etc/cron-min/5 # Executes everything in that directory every 5 minutes" echo " /etc/cron-min/13 # Executes everything in that directory every 13 minutes" echo " /etc/cron-min/90 # Executes everything in that directory every 90 minutes" echo exit fi for dir in $basedir/* ; do minutes=${dir##*/} if [ $(( ($(date +%s) / 60) % $minutes )) -eq 0 ] then for program in $basedir/$minutes/* ; do if [ -x $program ] then $program &> /dev/null & fi done fi done
*/10 * * * * root perl -e 'exit(time()%(90*60)>60)' && command
90分钟,这是一个半小时
“> 60” – 我给予cron一分钟内延迟脚本启动的能力
还有这个黑客的帮助,你可以设置任何一个分钟的分辨率
例如,每71分钟启动脚本
* * * * * root perl -e 'exit(time()%(71*60)>60)' && command
你也可以使用fcron ,它也可以接受更复杂的时间规格,比如:
@ 01h30 my_cmd
如果您计算Epoch以来的分钟数(分钟,小时,天或周),则可以达到任何频率,将条件添加到脚本的顶部,并将脚本设置为在crontab上每分钟运行一次:
#!/bin/bash minutesSinceEpoch=$(($(date +'%s / 60'))) # every 90 minutes (one and a half hours) if [[ $(($minutesSinceEpoch % 90)) -ne 0 ]]; then exit 0 fi
date(1)
返回当前date,我们将其格式化为Epoch( %s
)以来的秒数,然后我们执行基本的math运算:
# .---------------------- 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 7 hours if [[ $(($hours % 7)) -ne 0 ]]; then exit 0 fi # and your actual script starts here
添加以下内容到我的crontab并正在工作
15 */1 * * * root /usr/bin/some_script.sh >> /tmp/something.log