Why monitor
If you hang up, the phone will blow up.
In the official documentation of swool, it is written:
In some cases, if the system load is too heavy, the swoole cannot apply for memory and hangs up, a segmentation fault occurs at the bottom of the swoole, the server occupies too much memory and is killed by the kernel, or killed by some programs. The swoole-server will be unable to provide services, resulting in business interruption and loss of company revenue.
Using the process method to detect can directly use the official case to click to enter
Use the form of listening port to restart the swool service
The principle is similar to the monitoring process. Every 1 minute to monitor whether the port exists, if it does not exist, the swool service in the project is started.
check-port-on-crontab.sh
port=9502
#根据端口号查询对应的pid
pid=$(netstat -nlp | grep :$port | awk '{print $7}' | awk -F"/" '{ print $1 }');
#杀掉对应的进程,如果pid不存在,则不执行
if [ -z "$pid" ]; then
/usr/local/php/bin/php /www/im/swoole/im.php
fi
The current port is 9502, kill this port, and then execute the sh file. If the service is normal, the monitoring is normal.
join crontab
crontab -e
*/1 * * * * sh /www/sh/check-port-on-crontab.sh>/www/log/check-swool-port.log 2>&1 &
Execute the script check-port-on-crontab.sh every minute. At the same time, it is added to the custom log log.