期望目标:定时器一分钟执行一次,进行tomcat进程的检测,如果检测到tomcat关闭,则自动重启tomcat
步骤一:编写shell脚本
#!/bin/sh
# 获取tomcat进程ID /usr/local/tomcat_ds_api
TomcatID=$(ps -ef |grep tomcat |grep -w 'tomcat'|grep -v 'grep'|awk '{print $2}')
# tomcat启动程序(这里注意tomcat实际安装的路径)
StartTomcat=/home/XXX/XXX/apache-tomcat-7.0.64/bin/startup.sh
# 日志输出
TomcatMonitorLog=/tmp/TomcatMonitor.log
Monitor()
{
echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]"
if [[ $TomcatID ]];then
echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..."
else
echo "[error]tomcat进程不存在!tomcat开始自动重启..."
echo "[info]$StartTomcat,请稍候......"
#rm -rf $TomcatCache
$StartTomcat
fi
echo "------------------------------"
}
Monitor>>$TomcatMonitorLog
将此脚本保存为:monitor.sh
步骤二:编辑定时执行任务
1、输入命令:crontab -u root -e
2、添加任务:*/1 * * * */home/ddtech/dcxg/monitor/monitor.sh >> /tmp/TomcatMonitor.log
3、查看添加的任务:crontab -u root -l
4、重启定时任务管理:/sbin/service crond restart
这样,定时自动重启tomcat就写好了,看看日志有没有输出!
目标达成!!!!!!!!!!!!!!!!!!!!!