linux

如何监控LNMP运行状态

小樊
42
2025-04-05 03:39:24
栏目: 编程语言

要监控LNMP(Linux, Nginx, MySQL, PHP)环境的运行状态,可以采用以下几种方法:

使用系统监控工具

检查服务状态

分析日志文件

使用监控工具

自动化脚本

#!/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环境的运行状态,确保服务的稳定性和可靠性。

0
看了该问题的人还看了