简体   繁体   English

PHP脚本无法在命令行上运行

[英]PHP Script won't run on command line

The Code: 编码:

#!/usr/bin/php -q
<?php
while(true){
    $rate_limit = json_decode(file_get_contents('http://api.twitter.com/1/account/rate_limit_status.json'),true); # Check how many API calls remain

    $wait = round(3600/150);
    # *** IMPORTANT ***
    # Twitter limits API calls to 150/hr so regardless of the number of handles, we can only make one request every 24 seconds (which is 3600/150).
    # As a result, it takes one hour to run through 150 handles one time each.  Since we'll probably never monitor that many, each handle may get updated multiple times an hour.

    # Establish the database connection
    if(!$mysqli = mysqli_connect('localhost','twitterd','password','twitterd')){
        file_put_contents('tweet_gremlin.log', date('r') . ' Cannot connect to the database');
        break;
    }

    $get_handles = mysqli_query($mysqli,"SELECT `handle` FROM twitter_handles"); # Grab the Twitter handles from the database

    #  Append them to the $handles array
    while($row = mysqli_fetch_array($get_handles,MYSQLI_NUM)){
        $handles[] = $row[0];
    }

    #  Check the remaining API calls
    if($rate_limit['remaining_hits']<count($handles)){
        file_put_contents('tweet_gremlin.log', date('r') . ' Rate limit reached', FILE_APPEND);
        sleep(600);
        continue;
    }else{
        # Loop through the $handles values, make an API call, and insert the tweets.
        foreach($handles as $value){
            file_put_contents('tweet_gremlin.log', date('r') . ' Processing ' . $value . '\'s data...' . PHP_EOL, FILE_APPEND); # Tell the log what we're doing

            $user_data = json_decode(file_get_contents('http://search.twitter.com/search.json?q=from:' . $value . '&rpp=100&include_entities=1'),true); # Get the handle's timeline and put it into $user_data
            $user_data = $user_data['results']; # Put only the results index (tweets) into into $user_data

            if(count($user_data)<1){
                file_put_contents('tweet_gremlin.log', date('r') . ' No data for ' . $value . PHP_EOL, FILE_APPEND);
            }

            for($i=0;$i<count($user_data);$i++){
                # Lazy method for sanitizing variables
                $id = mysqli_real_escape_string($mysqli,$user_data[$i]['id']);
                $created_at = mysqli_real_escape_string($mysqli,$user_data[$i]['created_at']);
                $from_user_id = mysqli_real_escape_string($mysqli,$user_data[$i]['from_user_id']);
                $profile_image_url = mysqli_real_escape_string($mysqli,$user_data[$i]['profile_image_url']);
                $from_user = mysqli_real_escape_string($mysqli,$user_data[$i]['from_user']);
                $from_user_name = mysqli_real_escape_string($mysqli,$user_data[$i]['from_user_name']);
                $text = mysqli_real_escape_string($mysqli,$user_data[$i]['text']);

                $needles = array('hack','tango down','dump','breach','data');

                # Check the tweet relevance
                foreach($needles as $needle){
                    $needle = '/' . $needle . '/i'; # So we don't have to manually type out the regex

                    # Make a call based on the tweet contents
                    if(preg_match($needle,$text) == 0){
                        file_put_contents('tweet_gremlin.log', date('r') . ' No relevant tweet data in tweet #' . $id .PHP_EOL, FILE_APPEND);
                    }else{
                        file_put_contents('tweet_gremlin.log', date('r') . ' Processing tweet #' . $id .PHP_EOL, FILE_APPEND);
                        $insert_tweets = "INSERT INTO tweets (`id`,`created_at`,`from_user_id`,`profile_image`,`from_user`,`from_user_name`,`text`) VALUES ('{$id}','{$created_at}','{$from_user_id}','{$profile_image_url}','{$from_user}','{$from_user_name}','{$text}');";
                        mysqli_query($mysqli,$insert_tweets);
                    }
                }
            }
            file_put_contents('tweet_gremlin.log', date('r') . ' Sleeping for ' . $wait . ' seconds before processing the next handle' .PHP_EOL, FILE_APPEND);
            sleep($wait);
        }
    }
    mysqli_close($mysqli);
}
?>

If I run this on the command line like this: 如果我在这样的命令行上运行此命令:

./tweet_gremlin.php

It runs fine. 运行正常。 But if I run it like this: 但是,如果我这样运行它:

./tweet_gremlin.php &

It does nothing. 它什么也没做。 I cannot figure out why. 我不知道为什么。

EDIT: I had to take out pretty much all of the code because it wouldn't let me submit it all. 编辑:我不得不拿出几乎所有的代码,因为它不会让我全部提交。

You create a background job with the ampersand. 您使用&号创建一个后台作业。

Depending on the underlying OS the background job changes to status "Stopped" because it wants to read from STDIN and/or wants to write to STDOUT. 根据底层操作系统,后台作业将更改为“已停止”状态,因为它想从STDIN读取和/或要写入STDOUT。

Enter jobs into the same terminal after starting ./tweet_gremlin.php & 启动./tweet_gremlin.php &后,将jobs输入到同一终端
This tells your job's status. 这说明您的工作状态。

If the job's status is "Stopped" discard STDOUT+STDERR and STDIN: ./tweet_gremlin.php </dev/null &>/dev/null & 如果作业的状态为“已停止”,则丢弃STDOUT + STDERR和STDIN: ./tweet_gremlin.php </dev/null &>/dev/null &

If your script runs now you need to handle STDIN and/or STDOUT+STDERR differently in ./tweet_gremlin.php & 如果您的脚本现在运行,则需要在./tweet_gremlin.php &不同的方式处理STDIN和/或STDOUT + STDERR ./tweet_gremlin.php &
Eg write to a logfile instead of STDOUT+STDERR. 例如,写入日志文件而不是STDOUT + STDERR。

You can also test the behavior of your terminal. 您还可以测试终端的行为。
The following creates a PHP error on STDERR due to a missing semicolon: 由于缺少分号,以下代码会在STDERR上创建PHP错误:

php -r 'echo "backgrounding sucks\n"' &
jobs
fg1

The follwing creates normal output on STDOUT: 跟随者在STDOUT上创建正常输出:

php -r 'echo "backgrounding sucks\n";' &
jobs
fg1

If you cannot fix the issue in your code, look into your terminal's settings. 如果您无法在代码中解决问题,请查看终端的设置。 Watch out for stty tostop (the SIGTTOU signal) 提防停转(SIGTTOU信号)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM