dqy27359 2015-03-07 06:45
浏览 37
已采纳

在EC2微实例上连续运行Phirehose

I want to run Phirehose, a php Twitter streaming API on Amazon EC2 to run continuously. I used the following code

<?php
// TweetStream.php
require_once "OauthPhirehose.php";
require_once "../ajax/common.php";

const table_name = "tweets";

set_time_limit(0);

class Consumer extends OauthPhirehose {
    private $cobj; // mysqli object

    protected $count = 0;

    public function __construct($a, $b, $c) {
        parent::__construct($a, $b, $c);
        $this->cobj = new mysqli(db_host, db_user, db_pass, db_name);
    }

    public function enqueueStatus($status) {
        $data = json_decode($status);

        $cond = !isset($data->delete) && !isset($data->warning);
        $cond = $cond && !($data->id == 0 || trim($data->user->name) == "" || trim($data->text) == "" || $data->geo == NULL);
        if($cond) {
            ++$this->count;

            echo str_pad($this->count, 12, " ", STR_PAD_LEFT).": Getting Tweet: ".$data->id_str." Inserting... ";

            $id = $data->id_str;
            $idi = $data->id;
            $user = $data->user->name;
            $coord = $data->coordinates->coordinates;
            $lat = $coord[1];
            $lng = $coord[0];
            $tweet = $data->text;

            $stmt = $this->cobj->prepare("insert into ".table_name." values(?, ?, ?, ?, ?)");
            $stmt->bind_param("ssdds", $id, $user, $lat, $lng, $tweet);
            $stmt->execute();

            if($stmt->errno) echo "skipped: ".$stmt->error;
            else echo "done. 
";

            $stmt->close();
        }
    }
}

define("TWITTER_CONSUMER_KEY", "******");
define("TWITTER_CONSUMER_SECRET", "******");

define("OAUTH_TOKEN", "******");
define("OAUTH_SECRET", "********");

$sc = new Consumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_SAMPLE);
$sc->setLang("en");
$sc->consume();
?>

Once I upload this code to the EC2 instance I open ssh using Putty and execute following commands

$ cd /var/www/html/service
$ nohup php TweetStream.php &

After 5 minutes or so, when I run top -p $(pgrep -d',' php), I get the following

top - 06:38:52 up 1 day,  4:04,  1 user,  load average: 64.61, 66.82, 65.65
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):100.0%us,  0.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    606528k total,   599552k used,     6976k free,     1172k buffers
Swap:        0k total,        0k used,        0k free,    29660k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11902 ec2-user  20   0  438m 9096 1792 S  0.0  1.5   0:06.18 php

I can gather that it has run for 6 minutes or so and got into sleep state. Can I please know what am I missing?

How to make this process run continuously on Amazon EC2 Instance? Please help me friends.

By the way, The mysqli is connecting to RDS instance running in Amazon.

  • 写回答

1条回答 默认 最新

  • dongqichang7988 2015-03-07 17:34
    关注

    Never mind, running with screen instead nohup did the trick. I followed the tutorial here: https://unix.stackexchange.com/questions/479/keep-ssh-sessions-running-after-disconnection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计