drrrdo0802
drrrdo0802
2014-12-21 17:05

在特定日期安排php脚本,只需在Openshift中运行一次

  • php
  • openshift
  • cron
已采纳

I need to develop a web application to let me scheduled tasks at certain date. I have checked cron jobs and looks like is just for periodic tasks. In the other hand I just need schedule a single task at certain date without repetition.

Should I use cron jobs?

PD: a task is a php script.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanfen9090 duanfen9090 7年前

    According to this fantastic tutorial about Cron in openshift I know how use it.

    Depends on the accuracy you want to achieve.

    In example, in .openshift/cron/minutely/certain_date.sh

    #!/bin/bash
    DATE=`date +%D` #MM/DD/YY
    HOUR=`date +%H`
    MINUTE=`date +%M`
    
    
    if [ "$DATE" == "12/21/14" -a "$HOUR" == "23" -a "$MINUTE" == "15" ];then
        (
            #date >> $CUSTOM_REPO_DIR/certain_date.txt
            cd $CUSTOM_REPO_DIR ; #/usr/local/zend/bin/php
            $CUSTOM_PHP cron_certain_date.php
        )
    fi
    
    点赞 评论 复制链接分享
  • doubangzhang6992 doubangzhang6992 7年前

    You can create a php script scheduling your tasks and he's executing every hour with cron :

    0 * * * * /the/scheduling/script
    

    And the script would use a file to stock the tasks and dates like that :

    date task
    date task
    ...
    

    Or if you want you can stock your tasks in a database, it would be much simpler to manage (to delete/modifiy/add tasks)

    The php scheduling script who's executing every hour will check if today's date matches any of the tasks dates then execute the appropriate task for today if it exists.

    And if you want to add tasks, you can choose the scheduling script to receive the tasks with a GET field or receive them as parameters when calling the script from the command line (like that : php scheduling_script.php 2014-12-12 /path/to/thetask.php)

    点赞 评论 复制链接分享
  • dragonfly9527 dragonfly9527 7年前

    The way I've done this in the past is to use a cron job to schedule the task, then some logic inside the php script to ensure it's only run once.

    So, if you want your script to only run next Monday, you'd create a cron entry like

    1 1 * * mon /my/script/name
    

    This will run the script at 1:01 on every Monday while the crontab entry still exists.

    Then, within the php script, check that the day you're running on is the one you want. If not, bail out of the script.

    <?php
    // Ensure we're on our target execution date
    // Cron is set up to only run this once on Mondays, so we only
    // need to check the date
    if (date('Y-m-d') != '2014-12-22') {
        exit;
    }
    
    // Rest of script continues...
    

    Once the job has run, you can go back and remove the crontab entry at your leisure.

    点赞 评论 复制链接分享