原理是每 1 分钟执行一次 Shell 脚本,检测 server 的 master 进程是否存活,如果存在则跳过。如果发现主进程已经挂掉,则执行 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
netstat -lnp
检测端口是否在监听,如果未在监听,则执行 restart
check.php
发送一段带有逻辑的请求,试探服务器是否可以正常工作,如果无法工作,执行 restartsupervisor
监控进程的工具docker run
时增加参数 --restart=always