如何设置cron每隔一个半小时运行某些命令?
答案 0 :(得分:35)
普通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 :(得分:11)
有充分理由说你不能使用1小时或2小时吗?这肯定会更简单。
我个人没试过,但你可以在这里找到一些关于让cron每90分钟运行一次的信息:http://keithdevens.com/weblog/archive/2004/May/05/cron
以上链接的例外情况:
0 0,3,6,9,12,15,18,21 * * * <commands>
30 1,4,7,10,13,16,19,22 * * * <commands>
答案 2 :(得分:5)
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
答案 3 :(得分:1)
您可以使用两个crontab条目来完成此操作。每个小时每三个小时运行一次,它们偏移90分钟,如下所示:
0 0,3,6,9,12,15,18,21 * * *
30 1,4,7,10,13,16,19,22 * * *
答案 4 :(得分:1)
#! /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
答案 5 :(得分:1)
*/10 * * * * root perl -e 'exit(time()%(90*60)>60)' && command
&#34;&GT; 60&#34; - 我让cron能够在一分钟内延迟脚本的启动
在这个黑客的帮助下,您可以设置任何具有分钟分辨率的周期
例如,每71分钟启动一次脚本
* * * * * root perl -e 'exit(time()%(71*60)>60)' && command
答案 6 :(得分:0)
您还可以使用fcron,它也接受更复杂的时间规范,例如:
@ 01h30 my_cmd
答案 7 :(得分:0)
如果计算自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)
返回当前日期,我们将其格式化为自Epoch(%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 7 hours
if [[ $(($hours % 7)) -ne 0 ]]; then
exit 0
fi
# and your actual script starts here
答案 8 :(得分:-2)
将以下内容添加到我的crontab中并正在运行
15 */1 * * * root /usr/bin/some_script.sh >> /tmp/something.log