美文网首页
服务监控

服务监控

作者: HueyYao | 来源:发表于2020-12-28 22:21 被阅读0次

服务监控

利用swoole 通过linux来实现服务监控

通过模拟shell命令并使用swoole方法达到2秒执行一次的效果

<?php
/**
 * 监控服务 ws http 8812
 * Created by PhpStorm.
 * User: hueyyao
 * Date: 2020/12/29
 * Time: 下午10:00
 */
class Server {
    const PORT = 8812;
    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;
        }
    }
}
// 每2秒执行一次
swoole_timer_tick(2000, function($timer_id) {
    (new Server())->port();
    echo "time-start".PHP_EOL;
});

以上数据 均实在启动时现实的 我们可以讲其直接发送保存至文件中

可以使用linux命令 nohup来执行

nohup php server.php >需要保存的地址 &

nohup /usr/bin/php /app/thinkphp/script/monitor/server.php > /app/thinkphp/script/monitor/a.txt &

这是就已近再后台执行了 如果想关闭 可以执行

ps -ef 找到对应进程 执行后显示如下

root       343     1  0 22:12 pts/0    00:00:00 /usr/bin/php /app/thinkphp/script/monitor/server.php

通过显示得知PID为343

再输入 kill -9 343 后即可关闭

相关文章

网友评论

      本文标题:服务监控

      本文链接:https://www.haomeiwen.com/subject/lnoqoktx.html