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

时间:2018-05-07 15:59:54

标签: linux cron cron-task

如何每隔5小时运行Cron个工作?

我试过这个

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

但是这个expression的问题是每次5 hour都会运行,但时钟会在每天午夜00:00:00重置。

说明:如果作业在午夜00:00:00执行,那么以下将是执行时间

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

20:00:00作业将在00:00:00执行,1 hour比指定时间早00:00:00

但我的要求是工作应该以5小时的间隔运行,不应该在午夜重置

因此,如果作业在午夜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

如何通过var arr = [ obj = { name: "foo", Value: 2 }, obj = { name: "foo", Value: 5 }, obj = { name: "foo", Value: 2 }, obj = { name: "bar", Value: 2 }, obj = { name: "bar", Value: 1 } ] // chain the sequence of operations var result = _.chain(arr) // group the array by name .groupBy('name') // process each group .map(function(group, name) { // calculate the average of items in the group var avg = (group.length > 0) ? _.reduce(group, function(sum, item) { return sum + item.Value }, 0) / group.length : 0; return { name: name, value: avg } }) .value(); console.log(result);实现以下目标? 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

this forum post on unix.com中的一个选项每小时运行一次,然后添加到您的脚本中:

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

我做过类似于“每隔一个星期一”的类型逻辑。每个星期一运行脚本,只允许在脚本中的每个其他脚本上执行。