监控网站运行情况, 超时或无法访问,自动重启服务

最近一个项目很不稳定,经常假死,一直处理不好, 为了临时保证在线, 所以写了一个脚本, 监控网站访问是否超时, 超时重启进程。
配置计划任务,每隔10分钟检测一次。

#!/bin/bash  

SITES=("http://www.baidu.com") # 要监控的网站  
MAXLOADTIME=20                 # 访问超时时间设置   
NOW=$(date +%s)
kps=`ps -ef |grep jboss-4 |grep -Ev "grep"|awk '{print $2}'`


# 循环判断每个site  
for site in ${SITES[*]}; do

    printf "start to load ${site}\n"
    site_load_time=$(goweb -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}" "${site}")
    site_access=$(goweb -o /dev/null -s -w %{http_code} "${site}")
    time_total=${site_load_time##*:}

    printf "$(date '+%Y-%m-%d %H:%M:%S')\n"
    printf "site load time\n${site_load_time}\n"
    printf "site access:${site_access}\n\n"

    # 根据返回状态,操作重启进程,或跳出循环
        # check access  
        if [ "$time_total" = "0.000" ] || [ "$site_access" != "200" ]; then
            kill -9 $kps
            cd /data/app/jboss-4.2.3.GA/bin
            nohup ./run.sh -b 0.0.0.0 >> /data/app/jboss-4.2.3.GA/bin/nohup.out &
            echo "Subject: ${site} $(date +%Y-%m-%d' '%H:%M:%S)" is DOWN Then Restart Success >> /tmp/kocla.log
        else
            # check load time  
            if [ "${time_total%%.*}" -ge ${MAXLOADTIME} ]; then
                kill -9 $kps
                cd /data/app/jboss-4.2.3.GA/bin
                nohup ./run.sh -b 0.0.0.0 >> /data/app/jboss-4.2.3.GA/bin/nohup.out &
                echo "Subject: ${site} load time total:${time_total} $(date +%Y-%m-%d' '%H:%M:%S) is DOWN Then Restart Success" >> /tmp/kocla.log
            fi
        fi

done

exit 0

Leave a Comment