如何每隔5小时运行一次cron作业 [英] How to run a cron job at every 5 hour interval

查看:2234
本文介绍了如何每隔5小时运行一次cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何每隔5小时运行一次Cron作业?

How to run a Cron job at every 5 hour interval?

我已经尝试过

0 */5 * * * /path/to/script/script.sh (Not necessarily a shell script)

但是expression的问题是它将在每个5 hour上运行,但是时钟每天都会在午夜00:00:00重置.

But the problem with this expression is it'll run every 5 hour but the clock will reset at midnight 00:00:00 everyday.

说明::如果作业在00:00:00午夜执行,则以下为执行时间

Explanation: if the job executes at midnight 00:00:00 then following will be the execution timing

00:00:00, 05:00:00, 10:00:00, 15:00:00, 20:00:00

20:00:00之后,作业将在00:00:00处执行,该时间比指定的时间早1 hour.

after 20:00:00 the job will execute at 00:00:00 which is 1 hour earlier than specified.

但是我的要求是,该作业应每5小时运行一次,并且不应在午夜重置

But my requirement is the job should run at 5 hour interval and shouldn't get reset at midnight

因此,如果作业在00:00:00午夜开始,则应为以下执行顺序.

So if the job starts at midnight 00:00:00 the following should be the executing order.

00:00:00, 05:00:00, 10:00:00, 15:00:00, 20:00:00, (Day 1)
01:00:00, 06:00:00, 11:00:00, 16:00:00, 21:00:00, (Day 2)
02:00:00 ...                                      (Day 3)

如何通过Cron实现以下目标? 任何帮助,将不胜感激.

How do I achieve the following through Cron? Any help would appreciated.

推荐答案

One option from this forum post on unix.com Run it every hour, and add to your script:

time_ct=$(cat /tmp/cron_time_ct)
if [ $time_ct -lt 5 ]
   then
     echo "Not yet time"
     time_ct=$((time_ct+1))
     echo $time_ct > /tmp/cron_time_ct
     exit
   else
     time_ct=0
     echo $time_ct > /tmp/cron_time_ct
fi
# rest of your task

我做过类似于每隔星期一"类型的逻辑.每个星期一运行一次脚本,并且只允许在脚本中的每个其他脚本上执行.

I've done similar to do "every other monday" type logic. Running the script every monday and only allowing execution on every other one in the script.

这篇关于如何每隔5小时运行一次cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆