我想在 Amazon EC2 上运行 Phirehose,一个 php Twitter 流媒体 API,以便连续运行。我使用了以下代码
<?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. n";
$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();
?>
将此代码上传到 EC2 实例后,我使用 Putty 打开 ssh 并执行以下命令
$ cd /var/www/html/service
$ nohup php TweetStream.php &
大约 5 分钟后,当我运行top -p $(pgrep -d',' php)
时,我得到以下内容
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
我可以收集到它已经运行了 6 分钟左右并进入睡眠状态。我能知道我错过了什么吗?
如何使此过程在 Amazon EC2 实例上持续运行?请帮助我的朋友。
顺便说一下,mysqli正在连接到在Amazon中运行的RDS实例。
关系,用screen
跑步代替nohup
做到了。我按照这里的教程进行操作:https://unix.stackexchange.com/questions/479/keep-ssh-sessions-running-after-disconnection