dongshao9106 2016-10-19 21:58 采纳率: 100%
浏览 45
已采纳

使用PHP设置一个cron作业并永远重新访问

I have a file on the server that extract some data from pages, like a crawler. Ok, now, sometimes, the execution of the script takes 5 seconds, but sometimes takes even 1 minute or 2.

Setting a cron job for accessing the file at 2, 3, or 5 minute is not comfortable for me, because I need this crawler to run as fast as possible.

So my question is:

Can I set a cron job to run, let's say, every 5 minutes and set php to re-run the script again and again and again ?

More clear:

*/5 * * * * wget -O - site.com/index.php?cron >/dev/null 2>&1

index.php?cron

function cron_action()
{
    //some action here

    // Call again the function
    cron_action();
}

cron_action();

As I don't understand very well how does cron job react at my script, I don't know either what will happen when, on another 5 minutes, the cron job will acces the url again.

I will be in a infinity loop ?

How you would do that ? I need some advices please. I really need to set the cron job run faster and, in my opinion, the functions from php must be recalled forever.

  • 写回答

2条回答 默认 最新

  • doucuo8618 2016-10-19 22:16
    关注

    Setup cronjob:

    * * * * * php /path/to/your/file.php --arg1=value1 --arg2=value2 >> /dev/null 2>&1
    

    Your file.php

    <?php
    
    //test your args
    var_dump($argv);
    
    $minute = date('i',time());
    
    //run every 10 minutes
    if(intval($minute)%10==0){
        //run your cron job code here
        cron_action();
    }
    else{
        //do other code
    }
    
    function cron_action()
    {
        //do stub
    }
    

    And this is how to use cronjob with laravel https://laravel.com/docs/5.3/scheduling, you will learn from that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部