Swoole 服务器如何做到无人值守

| 选择喜欢的代码风格  

Swoole 无人值守 100% 可用原理:


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

#swoole restart
* * * * * /Data/webapps/check_swoole_server.sh

check_swoole_server.sh 代码参考如下:

#!/bin/bash

#count=`netstat -nutl | grep 9501 | grep "LISTEN" | wc -l`
count=`ps -fe |grep "swoole_server.php" | grep -v "grep" | wc -l`
echo $count

if [ $count -lt 1 ]; then
ps -eaf |grep "swoole_server.php" | grep -v "grep"| awk '{print $2}'|xargs kill -9
cd /Data/webapps/Swoole/

sleep 2
ulimit -c unlimited

/Data/apps/php7.3.8/bin/php swoole_server.php
echo "restart";
echo $(date +%Y-%m-%d_%H:%M:%S) >/Data/logs/restart.log
fi

Swoole 无人值守 100% 其他方式:


  • 可以通过 netstat -lnp 检测端口是否在监听,如果未在监听,则执行 restart
  • 通过一个 check.php 发送一段带有逻辑的请求,试探服务器是否可以正常工作,如果无法工作,执行 restart
  • 使用 supervisor 监控进程的工具
  • 如果在 Docker 容器中使用,可以在 docker run 时增加参数 --restart=always

Swoole 无人值守 100% 参考:




发表评论