运行一个不确定的PHP脚本一个小时.每小时重启一次


Run an indefinite PHP script for an hour. Restart every hour

我编写了一个PHP脚本从Twitter消防水带中提取tweet并将其存储到数据库中。理想情况下,我只想让它运行,以便随着时间的推移收集推文,因此,它被包装在while(1)循环中。

这似乎是有问题的,因为它超时了。如果我只是在浏览器中运行它,它不会运行超过30秒,然后超时并给我一个324错误。

问题:是否有一种方法,我可以让它运行一定的时间(20秒),自动杀死自己,然后重新启动?一切都在一个cron的工作(PS…)我不知道如何写一个cron作业)?

背景:站点托管在Godaddy。理想情况下,我想在我的主机服务器上运行这个。

脚本:

<?php
    $start = time();
    $expAddress = "HOSTNAME";
    $expUser = "USERNAME";
    $expPwd = "PASSWORD";
    $database = "DBNAME";
    $opts = array(
        'http' => array(
            'method'    =>  "POST",
            'content'   =>  'keywords,go,here',
        )
    );
    // Open connection to stream
    $db = mysql_connect($expAddress, $expUser, $expPwd);
    mysql_select_db($database, $db);
    $context = stream_context_create($opts);
    while (1) {
        $instream = fopen('https://USERNAME:PASSWORD@stream.twitter.com/1/statuses/filter.json','r' ,false, $context);
        while(! feof($instream)) {
             if(time() - $start > 5) { // break after 5 seconds
                break;
             }

            if(! ($line = stream_get_line($instream, 100000, "'n"))) {
                continue;
            }
            else {
                $tweet = json_decode($line);
                // Clean before storing             
                            // LOTS OF VARIABLES FOR BELOW...REMOVED FOR READABILITY
                // Send to database
                $ok = mysql_query("INSERT INTO tweets 
                    (created_at, from_user, from_user_id, latitude, longitude, tweet_id, language_code, 
                            place_name, profile_img_url, source, text, retweet_count, followers_count,
                            friends_count, listed_count, favorites_count) 
                    VALUES 
                    (NOW(), '$from_user', '$from_user_id', '$latitude', '$longitude', '$tweet_id', '$language_code', 
                            '$place_name', '$profile_img_url', '$source', '$text', '$retweet_count', '$followers_count',
                            '$friends_count', '$listed_count', '$favorites_count')");
                if (!$ok) { echo "Mysql Error: ".mysql_error(); }
                flush();
            }
        }
    }
?>

您可以设置每分钟运行一次cron作业。

按照以下步骤执行:

  1. 编写一个脚本运行PHP代码,例如:

    #!/bin/bash
    wget myurl.com/blah > /dev/null
    

    保存为my-cron.sh在某个文件夹(如/var)

  2. 添加到cron。执行crontab -e,参见Cron Format和Crontab usage。例如,它将每分钟运行一次。

    # Minute   Hour   Day of Month   Month   Day of Week    Command    
        *        *          *          *          *         /var/my-cron.sh
    

如果我得到你的需要,最好的事情是使用cron job使脚本无限期运行将不是一个好主意。

作为你的评论之一的说明符,你正在使用主机服务器Godaddy所以可能你将无法访问shell, 但是取决于你的cPanel版本,你可能能够创建和定义cron作业。

查看这个链接和这个Google搜索

也许,如果你没有这个选项,你愿意让浏览器打开,我建议如下

创建一个html页面作为客户端,每小时向PHP脚本发出一个ajax请求,像这样模拟cron作业函数

ajax请求代码可能看起来像(使用jQuery)

function makeRequest(){
    $.ajax({
        url: "http://yourhost/url-to-your-script.php",
        complete: function(data){
            setTimeout(function(){
                makeRequest();
            }, 60 * 60 * 1000); // Minutes * Seconds * MS
        }
    });
}
makeRequest();

我希望这对你有帮助

编辑

这个链接可能也有帮助

重要的不要忘记删除无限循环

I just had same issue.
Only cron job can do if you want run script off browser. You can set up cron job with free providers or you can set up cron job in windows's Scheduled tasks.
If your site has a good traffic then you can follow the option below that your users does the work for you.
In php you can find time in hour and seconds 
$time= date(' H:i:s');
create a table to track if the code was run.
eg; table column  name check with option 0 and 1;
select check from table.
    enter code here
if ($minute > 59)
{
if($check==0)
{
run your code
then update the table each time when it was run
eg; update table set check='1' 
}
}
then another if condition to reset your code
if(minute>0 && minute <1)
{
select check from your table.
if(check==1)
{
update table set check='0'
}
}