要监控LNMP(Linux, Nginx, MySQL, PHP)环境的运行状态,可以采用以下几种方法:
sudo systemctl status nginx
sudo systemctl status mysql
sudo systemctl status php-fpm
sudo tail -n 10 /var/log/nginx/error.log
sudo tail -n 10 /var/log/mysql/error.log
sudo tail -n 10 /var/log/php7.x/error.log
#!/bin/bash
# 获取当前时间
echo "$(date +'%Y-%m-%d %H:%M:%S')"
# 检查php-fpm服务是否运行
if ! netstat -an | grep php-cgi | wc -l; then
# 重启php-fpm服务
/etc/init.d/php-fpm restart
# 写入日志
echo "$(date +'%Y-%m-%d %H:%M:%S') php-fpm service is down... restart..." >> /home/checkfail-lnmp.log
fi
# 检查mysql服务是否运行
if ! netstat -anpt | grep mysqld | awk '{print $4}' | awk -F: '{print $2}' | wc -l; then
# 重启mysql服务
/etc/init.d/mysql restart
# 写入日志
echo "$(date +'%Y-%m-%d %H:%M:%S') mysqld service is down... restart..." >> /home/checkfail-lnmp.log
fi
# 检查nginx服务是否运行
if ! netstat -anpt | grep nginx | awk '{print $4}' | awk -F: '{print $2}' | wc -l; then
# 重启nginx服务
/etc/init.d/nginx restart
# 写入日志
echo "$(date +'%Y-%m-%d %H:%M:%S') nginx service is down... restart..." >> /home/checkfail-lnmp.log
fi
为脚本添加执行权限,并将其添加到crontab中,可以每分钟检查一次服务状态:
通过上述方法,可以有效地监控LNMP环境的运行状态,确保服务的稳定性和可靠性。