1.shell脚本编写
#!/bin/sh
TomcatID=$(ps -ef|grep tomcat|grep java|grep -v "grep"|awk '{print $2}')
StartTomcat=/usr/local/apache-tomcat-7.0.78/bin/startup.sh
TomcatCache=/usr/local/apache-tomcat-7.0.78/work
WebUrl=http://123.18.25.111:8080/
GetPageInfo=/dev/null
TomcatMonitorLog=/usr/local/shell/TomcatMonitor.log
Monitor()
{
echo "[info]start monitor tomcat...[$(date +'%F %H:%M:%S')]"
if [[ $TomcatID ]];then
echo "[info]current tomcat pid ID is:$TomcatID,go on check page..."
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
if [ $TomcatServiceCode -eq 200 ];then
echo "[info]tomcat run successful,and test page is normal......"
else
echo "[error]tomcat get error"
echo "[error]restart tomcat"
kill -9 $TomcatID
sleep 3
rm -rf $TomcatCache
$StartTomcat
fi
else
echo "[error]tomcat process in not exist! tomcat auto restart..."
echo "[info]$StartTomcat,please in wait......"
rm -rf $TomcatCache
$StartTomcat
fi
echo "------------------------------"
}
Monitor>>$TomcatMonitorLog
脚本中StartTomcat,TomcatID,根据自己tomcat位置及tomcat名自定义 ,WebUrl可以是一个比较小的页面,也可以是服务器的资源图片
2.window上编辑后的sh脚本在linux执行报错 原因:window上保存文件,换行是\r\n ,Linux上\n 解决方案 : 在linux 上用vim编辑器 打开sh脚本, 输入:set ff = unix 在输入 :wq 保存并退出
3.Linux 定时器使用
crontab -e 编辑定时器 详细定时器格式 可自行百度查看 crontab -l 可查看当前执行的定时任务 */1 * * * * /usr/local/shell/monitor.sh