How to schedule task with certain (say, 5) hour period?

Deadly 提交于 2019-12-12 06:36:36

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/30569621/how-to-schedule-task-with-certain-say-5-hour-period

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