php+swoole+linux进行系统监控和性能优化

服务器监控

  • 端口监控php运行shell脚本
class Server {
    const PORT = 8811;
    /**
     * 获取端口指定端口信息;如果在运行返回1;否则返回0;
     */
    public function port() {
        $shell  =  "netstat -anp 2>/dev/null | grep ". self::PORT . " | grep LISTEN | wc -l";

        $result = shell_exec($shell);
        if($result != 1) {
            // 发送报警服务 邮件 短信
            /// todo
            echo date("Ymd H:i:s")."error".PHP_EOL;
        } else {
            echo date("Ymd H:i:s")."succss".PHP_EOL;
        }
    }
}
/**
 * swoole毫秒定时器;每隔2秒运行一下脚本
 */
swoole_timer_tick(2000, function($timer_id) {
    (new Server())->port();
    echo "time-start".PHP_EOL;
});
  • linux在终端执行命令并且写入到文件里面

    nohup /usr/local/php/bin/php/www/swoole/thinkphp_swoole/script/monitor/server.php>/www/swoole/thinkphp_swoole/script/monitor/a.txt-**

  • 根据端口别名使用.sh(shell脚本)平滑重启服务

echo "loading..."
pid=`pidof live_master`
echo $pid
kill -USR1 $pid
echo "loading success"
  • 后台启动swoole

nohup /usr/local/php/bin/php /www/swoole/thinkphp_swoole/server/ws.php>/www/swoole/server/thinkphp_swoole/swoole.log &

你可能感兴趣的:(pHp,swoole,linux)