swoole服务器如何做到无人值守100%可用

版权声明:CopyRight @CSDN 码农Robin https://blog.csdn.net/weixin_41423450/article/details/86677705

在某些情况下,如系统负载过大swoole无法申请到内存而挂掉、swoole底层发生段错误、Server占用内存过大被内核Kill,或者被某些程序误杀。那swoole-server将无法提供服务,导致业务中断,公司收入出现损失。

有一个非常有效并且在BAT等大型公司常用的方案是crontab重启监控。

原理是每1分钟执行一次shell脚本,检测server的master进程是否存活,如果存在则跳过。如果发现主进程已经挂掉,则执行restart逻辑,先kill掉所有残留的子进程,然后重新启动Server。

  1. shell脚本

使用下面的脚本需要将Server程序的进程名称设置为master,如 cli_set_process_title(“php server.php: master”)

/data/script/check_server.sh:

count=`ps -fe |grep "server.php" | grep -v "grep" | grep "master" | wc -l`

echo $count
if [ $count -lt 1 ]; then
ps -eaf |grep "server.php" | grep -v "grep"| awk '{print $2}'|xargs kill -9
sleep 2
ulimit -c unlimited
/usr/local/bin/php /data/webroot/server.php
echo "restart";
echo $(date +%Y-%m-%d_%H:%M:%S) >/data/log/restart.log
fi

或者

count=`netstat -nutl | grep 9503 | grep "LISTEN" | wc -l`
#echo $count
if [ $count -lt 1 ];then
    nohup   php  /var/www/html/swoole_imooc/thinkphp/server/ws.php >> /var/log/swoole/swoole.log &
    echo "restart".$(date +%Y-%m-%d_%H:%M:%S) >> /var/log/swoole/restart.log
fi
  1. php脚本
class Monitor
{
    const PORT = 9503;

    public function swoole(){
        $shell = "netstat -ntul|grep ".self::PORT."| grep LISTEN | wc -l";
        $result = shell_exec($shell);
        if($result == 0){
            //报警,发送短信或者邮箱给负责人
            //nohup命令 以守护进程形式重启,并将重启结果写入到文件中
            $restart_shell = "nohup php /var/www/html/swoole_imooc/thinkphp/server/ws.php > /var/www/html/swoole_imooc/thinkphp/server/t.txt &";
            $time = date("Y-m-d H:i:s",time());
            $date = date("Ymd",time());
            $file = "find /var/log/swoole -name {$date}.log | wc -l";
            //记录到日志文件
            if(shell_exec($file) == 0){
                shell_exec("touch /var/log/swoole/{$date}.log");
            }
            //将重启时间追加到日志文件中
            $log ="echo {$time}.'|restart' >> /var/log/swoole/{$date}.log";
            shell_exec($log);
            shell_exec($restart_shell);
        }else{
            echo date("Y-m-d H:i:s",time()).'success';
        }
    }
}
//执行一次
$monitor = new Monitor();
$monitor->swoole();

将上述脚本加入到定时任务中,每分钟执行,即可实现swoole服务无人值守,100可用了。

猜你喜欢

转载自blog.csdn.net/weixin_41423450/article/details/86677705
今日推荐