监控逻辑为:监控nginx端口状态是否正常 以及 nginx进程号是否存在
监控脚本名称为nginx_monitor.sh,脚本内容如下
#!/bin/sh
MONITOR_LOG=/home/gavin2/scripts/log/nginx_monitor.log
nginx_monitor()
{
#nginx的端口号
PORT=“80”
#获取nginx端口监听状态,如果nginx正常运行,PORT_FLAG值为0
PORT_STATUS=$(netstat -plnt|grep $1|grep ${PORT})
#获取上一个命令的退出状态,正常退出为0
PORT_FLAG=$?
#使用管道命令查询nginx的进程号
NGINX_STATUS=$(ps -ef |grep $1|grep -v ‘grep’|grep master|awk ‘{print $2}’)
#如果进程号存在并且端口监听正常,则说明nginx正常运行
if [ ${NGINX_STATUS} ] && [ ${PORT_FLAG} = “0” ];then
echo date "+%Y/%m/%d %H:%M:%S $1 is running"
>>${MONITOR_LOG} 2>&1
elsehttp://groups.tianya.cn/post-191232-bec4a1fb6a574fdf806c9eaf8e9aa8e1-1.shtml
echo date "+%Y/%m/%d %H:%M:%S [error] $1 has already shutdown"
>>${MONITOR_LOG} 2>&1
fihttp:/