问题
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