脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - 自动重启服务的shell脚本代码

自动重启服务的shell脚本代码

2022-12-20 14:29shell教程网 shell

公司tomcat服务器有个过一段时间自动会挂的bug一直没能解决,挂的时候还要手动重启tomcat,于是决定写个脚本让它定时检测故障自动重启吧

复制代码 代码如下:


#!/bin/bash
if [ ! -f /tmp/down_count ];then
echo "0" > /tmp/down_count
fi
curl -I tomcat-host -o "/tmp/status" >/dev/null 2>&1
code=`awk 'NR==1 {print $2}' /tmp/status`
if [ "$[code]" -ge 500 ];then
down=`expr $(cat /tmp/down_count) + 1`
echo "$down" > /tmp/down_count
if [ "$down" -gt 3 ];then
if [ ! -f "/tmp/restart_count" ];then
echo "0" > /tmp/restart_count
fi
restart_count=`expr $(cat /tmp/restart_count) + 1`
echo "$restart_count" > /tmp/restart_count
if [ "$restart_count" -le 2 ];then
echo "tomcat down at `date`" >> /tmp/down_info
/etc/init.d/tomcat6 restart
fi
fi
else
echo "0" > /tmp/down_count
echo "0" > /tmp/restart_count
fi



脚本实现了,当检测网页状态码大于等于500连续出现3次数,自动重启tomcat6,且只连续重启两次。

延伸 · 阅读

精彩推荐