Swoole-服务进程实时监控

1.新建文件serivce.php

<?php
/**
 * Created by bingxiong.
 * Date: 5/25/18
 * Time: 5:40 PM
 * Description: 监控服务
 */

class Server{
    const PORT = 9000;

    public function port(){
        $shell = "netstat -an 2>/dev/null| grep 9000 | 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")."success".PHP_EOL;
        }
    }
}

//(new Server()) -> port();
//这里使用swoole的定时器每两秒执行一次检测
swoole_timer_tick(2000, function ($timer_id){
    (new Server()) -> port();
    echo "time-start".PHP_EOL;
});

 

说明:

  • 本质上就是执行一个shell脚本来监控端口,通过脚本的返回值来判断端口运行情况然后使用了swoole把没两秒调用一次把数据打出来
  • 进一步的可以把数据使用重定向写入文件(也可以在shell脚本中进行重定向)
 执行脚本:方法一
 nohup php server.php > /www/wwwroot/x.com/api/log.txt &

 查看脚本执行情况,找到运行脚本
 ps aux | grep php

 实时监控打印数据
 tail -f /www/wwwroot/x.com/api/log.txt

 

停止脚本使用:

ps aux | grep php 查看脚本的PID

然后kill -9 号码

posted @ 2022-07-02 22:28  快乐的在一起  阅读(285)  评论(0编辑  收藏  举报