How to run a cron job at every 5 hour interval

[亡魂溺海] 提交于 2019-12-31 07:09:25

问题


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

I've tried this

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

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

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

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

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

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)

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


回答1:


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.



来源:https://stackoverflow.com/questions/50218213/how-to-run-a-cron-job-at-every-5-hour-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!