如何每72分钟做一次cron工作

时间:2022-01-27 05:17:36

How would I get a cron job to run every 72 minutes? Or some not so pretty number like that?

如何每72分钟运行一次cron作业?还是有些不那么漂亮?

7 个解决方案

#1


Since cron runs jobs time-based, not interval-based, there's no blindingly simple way to do it. However, although it's a bit of a hack, you can set up multiple lines in crontab until you find the common denominator. Since you want a job to run every 72 minutes, it must execute at the following times:

由于cron以基于时间的方式运行作业,而不是以间隔为基础,因此没有简单明了的方法。但是,虽然它有点像黑客,但您可以在crontab中设置多行,直到找到共同点。由于您希望每72分钟运行一次作业,因此必须在以下时间执行:

  • 00:00
  • 01:12
  • 02:24
  • 03:36
  • 04:48
  • 06:00
  • 07:12
  • ...

As you can see, the pattern repeats every 6 hours with 5 jobs. So, you will have 5 lines in your crontab:

如您所见,该模式每6个小时重复5个作业。那么,你的crontab中将有5行:

0  0,6,12,18  * * * command
12 1,7,13,19  * * * command
24 2,8,14,20  * * * command
36 3,9,15,21  * * * command
48 4,10,16,22 * * * command

The other option, of course, is to create a wrapper daemon or shell script that executes and sleeps for the desired time until stopped.

当然,另一个选择是创建一个包装器守护程序或shell脚本,它执行并休眠所需的时间直到停止。

#2


Use at (man at). Have your app or startup script calculate a startup time 72 minutes in the future and schedule itself to run again before it starts working.

Available on windows xp and vista too.

Here's an example for gnu/linux: at -f command.sh now + 72 minutes

使用at(man at)。让您的应用程序或启动脚本在将来72分钟计算启动时间,并安排自己在开始工作之前再次运行。也适用于Windows XP和Vista。这是gnu / linux的一个例子:at -f command.sh现在+ 72分钟

#3


You could always take the approach of triggering cron every minute, and having your script exit out immediately if it's been run more recently than 72 minutes ago.

你总是可以采取每分钟触发cron的方法,如果它比72分钟前更早运行,你的脚本会立即退出。

#4


You cannot directly do this from cron/crontab.

您不能直接从cron / crontab执行此操作。

Cron jobs are run on a specific schedule, not on a specific interval.

Cron作业按特定计划运行,而不是按特定时间间隔运行。

One alternative would be to work out a schedule that approximated your "every 72 minutes" by running at midnight, 1:12, 2:24, 3:36, ..., and stretching it out to approximate hitting up at midnight. Your crontab file could specify all of these times as times to execute.

另一种方法是通过在午夜,1:12,2:24,3:36,......运行一个接近你的“每72分钟”的时间表,然后将其拉伸到午夜大约的时间。您的crontab文件可以指定所有这些时间来执行。

Another alternative would be to have a separate application handle the scheduling, and fire your application.

另一种方法是让一个单独的应用程序处理调度,并激活你的应用程序。

#5


You'll need to set exactly 20 tasks for this - i.e. set one at 00:00, next one at 01:12, next one at 02:24, etc.

你需要为此设置20个任务 - 即在00:00设置一个,在01:12设置下一个,在02:24设置下一个等等。

20 iterations make a full day.

20次迭代使一整天。

Unfortunately, this is the only way to do it, as cron tasks are set up in a fixed schedule beforehand instead of being run, say, "after X minutes the last task was executed".

不幸的是,这是唯一的方法,因为cron任务是事先在固定的时间表中设置而不是运行,比如“在X分钟后执行最后一个任务”。

#6


Don't use cron...

不要用cron ......

#!/bin/sh
while [ true ] 
do
     sleep 4320
     echo "Put your program here" &
done

#7


Uh I know this is long overdue, but I was looking at some scheduling issues and saw this question.

呃我知道这是姗姗来迟的,但我正在研究一些调度问题并看到了这个问题。

Just do this in your crontab

只需在您的crontab中执行此操作即可

*/72 * * * * /home/script.sh

* / 72 * * * * /home/script.sh

#1


Since cron runs jobs time-based, not interval-based, there's no blindingly simple way to do it. However, although it's a bit of a hack, you can set up multiple lines in crontab until you find the common denominator. Since you want a job to run every 72 minutes, it must execute at the following times:

由于cron以基于时间的方式运行作业,而不是以间隔为基础,因此没有简单明了的方法。但是,虽然它有点像黑客,但您可以在crontab中设置多行,直到找到共同点。由于您希望每72分钟运行一次作业,因此必须在以下时间执行:

  • 00:00
  • 01:12
  • 02:24
  • 03:36
  • 04:48
  • 06:00
  • 07:12
  • ...

As you can see, the pattern repeats every 6 hours with 5 jobs. So, you will have 5 lines in your crontab:

如您所见,该模式每6个小时重复5个作业。那么,你的crontab中将有5行:

0  0,6,12,18  * * * command
12 1,7,13,19  * * * command
24 2,8,14,20  * * * command
36 3,9,15,21  * * * command
48 4,10,16,22 * * * command

The other option, of course, is to create a wrapper daemon or shell script that executes and sleeps for the desired time until stopped.

当然,另一个选择是创建一个包装器守护程序或shell脚本,它执行并休眠所需的时间直到停止。

#2


Use at (man at). Have your app or startup script calculate a startup time 72 minutes in the future and schedule itself to run again before it starts working.

Available on windows xp and vista too.

Here's an example for gnu/linux: at -f command.sh now + 72 minutes

使用at(man at)。让您的应用程序或启动脚本在将来72分钟计算启动时间,并安排自己在开始工作之前再次运行。也适用于Windows XP和Vista。这是gnu / linux的一个例子:at -f command.sh现在+ 72分钟

#3


You could always take the approach of triggering cron every minute, and having your script exit out immediately if it's been run more recently than 72 minutes ago.

你总是可以采取每分钟触发cron的方法,如果它比72分钟前更早运行,你的脚本会立即退出。

#4


You cannot directly do this from cron/crontab.

您不能直接从cron / crontab执行此操作。

Cron jobs are run on a specific schedule, not on a specific interval.

Cron作业按特定计划运行,而不是按特定时间间隔运行。

One alternative would be to work out a schedule that approximated your "every 72 minutes" by running at midnight, 1:12, 2:24, 3:36, ..., and stretching it out to approximate hitting up at midnight. Your crontab file could specify all of these times as times to execute.

另一种方法是通过在午夜,1:12,2:24,3:36,......运行一个接近你的“每72分钟”的时间表,然后将其拉伸到午夜大约的时间。您的crontab文件可以指定所有这些时间来执行。

Another alternative would be to have a separate application handle the scheduling, and fire your application.

另一种方法是让一个单独的应用程序处理调度,并激活你的应用程序。

#5


You'll need to set exactly 20 tasks for this - i.e. set one at 00:00, next one at 01:12, next one at 02:24, etc.

你需要为此设置20个任务 - 即在00:00设置一个,在01:12设置下一个,在02:24设置下一个等等。

20 iterations make a full day.

20次迭代使一整天。

Unfortunately, this is the only way to do it, as cron tasks are set up in a fixed schedule beforehand instead of being run, say, "after X minutes the last task was executed".

不幸的是,这是唯一的方法,因为cron任务是事先在固定的时间表中设置而不是运行,比如“在X分钟后执行最后一个任务”。

#6


Don't use cron...

不要用cron ......

#!/bin/sh
while [ true ] 
do
     sleep 4320
     echo "Put your program here" &
done

#7


Uh I know this is long overdue, but I was looking at some scheduling issues and saw this question.

呃我知道这是姗姗来迟的,但我正在研究一些调度问题并看到了这个问题。

Just do this in your crontab

只需在您的crontab中执行此操作即可

*/72 * * * * /home/script.sh

* / 72 * * * * /home/script.sh