tomcat运行一段时间后,凌晨无缘无故挂掉,看了tomcat日志、项目日志、系统日志,没有发现错误。于是想到写一个shell脚本,每隔2分钟监控一次tomcat 的状态,若挂掉,则重新启动。解决方案参考网络,同时修改成符合自己的脚本。
使用环境
- 操作系统:CentOS 7
- JDK版本:1.8.0_161-b12 64位
- Tomcat版本:8.5.29
编写脚本
在win下新建:monitor.sh,内容如下:
1 | !/bin/sh |
在bin目录下执行:./monitor.sh(点不出的话先授权:sudo chmod a+x monitor.sh),发现报错,错误如下:
1 | -bash: ./monitor.sh: /bin/sh^M: bad interpreter: No such file or directory |
原因是因为在windows下编辑的,然后上传到linux系统里执行。.sh文件的格式为dos格式,而linux只能执行格式为unix格式的脚本 ,解决方法:
1 | 没有 需要先安装 |
添加任务
1 | 没有安装 需要先安装crontab |
crontab -e ,在文档末尾处添加(每隔2分钟执行一次):
1 | */2 * * * * /usr/local/hrfiles/apache-tomcat-8.5.29_2/bin/monitor.sh |
执行日志可查看:
1 | tail -f /tmp/TomcatMonitor.log |