#!/usr/local/bin/php
$type = $argv[1];
$ip = "10.10.10.110";
$worker_id = 1;
if ($type == "start") {
myexec("nsqlookupd -http-address=\"0.0.0.0:4161\" -tcp-address=\"0.0.0.0:4160\" -broadcast-address=\"$ip\" ");
foreach(array(4150, 5150) as $tcp_port) {
$http_port = $tcp_port + 1;
$data_dir = "/data1/nsq_data/$tcp_port";
if(!file_exists($data_dir)) {
mkdir($data_dir);
}
myexec("nsqd -data-path=$data_dir -http-address=\"0.0.0.0:$http_port\" -tcp-address=\"0.0.0.0:$tcp_port\" -broadcast-address=\"$ip\" -worker-id=$worker_id -lookupd-tcp-address=$ip:4160");
$worker_id++;
}
myexec("nsqadmin -http-address=\"0.0.0.0:4171\" -lookupd-http-address=localhost:4161");
myexec("nsq_pubsub -lookupd-http-address=localhost:4161 -http-address=:8181");
}
if ($type == "stop") {
mykill("nsqlookupd");
mykill("nsqd");
mykill("nsqadmin");
mykill("nsq_pubsub");
}
if ($type == "show") {
mygrep("nsqlookupd");
mygrep("nsqd");
mygrep("nsqadmin");
mygrep("nsq_pubsub");
}
function mygrep($name) {
echo `ps aux|grep $name|grep -v grep`;
}
function mykill($name) {
system("ps aux|grep $name|grep -v grep | awk '{print $2}'|xargs -n 1 kill -9");
}
function myexec($cmd) {
$arr = explode(" ", $cmd);
$name = $arr[0];
$pid = pcntl_fork();
if ($pid == 0) {
system("nohup $cmd >$name.log 2>&1 &", $ret);
if ($ret != 0) {
die("exec cmd fail: $cmd\n");
}
}
}