如何安排一定的时间(例如5小时)? [英] How to schedule task with certain (say, 5) hour period?

查看:146
本文介绍了如何安排一定的时间(例如5小时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用crontab安排任务是很常见的。但是,我发现crontab只能按每小时或每天安排任务。如果我想每4小时安排一次任务,我仍然可以通过设置6个日常任务来完成任务。但是,如果我想要有一段时间不能弥补一天,比如说,每5小时/ 7小时完成一项任务?有没有办法在crontab中按小时期间设置任务或使用其他工具? (如果该工具可以在像Heroku / Openshift这样的PaaS上运行,那会更好)

每分钟crontab设置一个任务,每5分钟发生一次( https://github.com/openshift-quickstart/openshift-cacti-quickstart/blob/master/.openshift/cron/minutely/cactipoll

  if [! -f $ OPENSHIFT_DATA_DIR / last_run];然后
touch $ OPENSHIFT_DATA_DIR / last_run
fi
if [[$(find $ OPENSHIFT_DATA_DIR / last_run -mmin +4)]];然后#每5分钟运行
rm -f $ OPENSHIFT_DATA_DIR / last_run
touch $ OPENSHIFT_DATA_DIR / last_run
php $ OPENSHIFT_REPO_DIR / php / poller.php> $ OPENSHIFT_DATA_DIR / cacti.log 2>& 1

fi

基本上该任务每分钟运行一次,但仅在5分钟后才能执行。你可以很容易地适应这个使用小时而不是分钟。


It is common to use crontab to schedule a task. However, I found that crontab can only schedule tasks on a minutely, hourly or daily basis. If I want to schedule task on every 4 hours, I can still make it by setting up 6 daily task. However, what if I want to have a period that doesn't make up one day, say, a task every 5 hours / 7 hours? Is there a way to set up the task by hour period in crontab or using other tools? (would be even better if that tool can be run on PaaS like Heroku / Openshift)

解决方案

Here is an example of using the minutely crontab to setup a task that happens every 5 minutes (https://github.com/openshift-quickstart/openshift-cacti-quickstart/blob/master/.openshift/cron/minutely/cactipoll)

if [ ! -f $OPENSHIFT_DATA_DIR/last_run ]; then
    touch $OPENSHIFT_DATA_DIR/last_run
fi
if [[ $(find $OPENSHIFT_DATA_DIR/last_run -mmin +4) ]]; then #run every 5 mins
    rm -f $OPENSHIFT_DATA_DIR/last_run
    touch $OPENSHIFT_DATA_DIR/last_run
    php $OPENSHIFT_REPO_DIR/php/poller.php > $OPENSHIFT_DATA_DIR/cacti.log 2>&1

fi

Basically the task runs every minute, but only gets executed after 5 minutes have passed. You can easily adapt this to use hours instead of minutes.

这篇关于如何安排一定的时间(例如5小时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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