31

如何设置 cron 每隔一个半小时运行一次某些命令?

4

10 回答 10

43

使用 normal 中的单个表达式是不可能的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]
于 2008-10-29T17:17:03.693 回答
16

为什么你不能使用 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>
于 2008-10-29T17:16:34.273 回答
8

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
于 2008-10-29T17:17:11.213 回答
4

您可以使用两个 crontab 条目来完成。每个每三个小时运行一次,它们会偏移 90 分钟,如下所示:

0 0,3,6,9,12,15,18,21 * * *

30 1,4,7,10,13,16,19,22 * * *

于 2008-10-29T17:17:55.360 回答
2

您还可以使用fcron,它也接受更复杂的时间规范,例如:

@ 01h30 my_cmd
于 2008-10-29T17:47:56.347 回答
2
#! /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
于 2014-02-17T04:21:19.493 回答
2

*/10 * * * * root perl -e 'exit(time()%(90*60)>60)' && command

90 - 以分钟计是一个半小时

"> 60" — 我让 cron 能够在一分钟内延迟脚本的启动

此外,借助此 hack,您可以设置任何具有分钟分辨率的时间段

例如每 71 分钟启动一次脚本

* * * * * root perl -e 'exit(time()%(71*60)>60)' && command

于 2014-08-27T22:30:27.457 回答
2

如果您计算自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
于 2015-08-01T06:40:51.910 回答
0

在 crontab 上使用“ at ”

该命令在午夜执行,然后设置为在 1.30 执行

ETC ..

0 */3 * * * echo "Command" | at now +90 minutes && Command

于 2022-01-10T12:58:40.083 回答
-3

将以下内容添加到我的 crontab 并且正在工作

15 */1   * * *   root    /usr/bin/some_script.sh >> /tmp/something.log
于 2014-06-27T10:15:55.010 回答