PHP+Swoole+Linux 实现进程监控 教程

1、Server.php 脚本代码:

class Server
{
    const PORT = 8888;

    public function port()
    {
        //netstat -anp 2>/dev/null| grep 8888 |grep LISTEN |wc -l
        $shellCommand = 'netstat -anp 2>/dev/null| grep ' . self::PORT . "| grep LISTEN | wc -l";
        $result = shell_exec($shellCommand);
        if ($result != 1) {
            //进程不存在,发送警报信息
            echo 'At '.date('Y-m-d H:i:s') . ' service process is exit' . PHP_EOL;
        } else {
            echo 'At '.date('Y-m-d H:i:s') . ' service process is ok' . PHP_EOL;
        }
    }

}

Swoole\Timer::tick(1000, function (int $timer_id) {
    (new Server)->port();
});

 

2、运行脚本代码

nohup /usr/bin/php ./Server.php > ~/test.log &

 

注意:实现更加精准的进程监控的写法

netstat -anp 2>/dev/null| grep 127.0.0.1:8866 | grep LISTEN | wc -l
    A+
发布日期:2019年07月22日 10:37:24  所属分类:Swoole
最后更新时间:2019-07-22 10:38:33
头像
  • ¥ 89.0元
  • 市场价:129.0元
  • ¥ 398.0元
  • 市场价:498.0元
  • ¥ 199.0元
  • 市场价:399.0元
  • ¥ 29.0元
  • 市场价:99.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: