Bash
1 前言
一个问题,一篇文章,一出故事。
笔者几天前发现Tomcat会因为一个错误而停止服务,虽然进程还在,但是服务已经无任何回应,详细原因如下,
笔者虽然有通知开发处理,但是修改可能有点延迟,于是笔者想给Tomcat写一个服务检查脚本,当异常时自动重启服务。
2 最佳实践
2.1 寻找异常的配置文件
vim ~/scripts/autoTomcatManager.sh
加入如下配置,
#!/bin/bash tomcatLog="/var/log/tomcat/autoTomcatManager.log" tomcatUrl="http://localhost:8080" tomcatKeywords="<title>Apache Tomcat/8.5.81</title>" tomcatUser="tomcat" tomcatExt="jsvc" if [ `curl -s "$tomcatUrl" | grep -i "$tomcatKeywords" | wc -l` = 1 ]; then exit fi echo `date +"%Y/%m/%d %H:%M:%S"`" Tomcat service failure!" >> "$tomcatLog" for ((;;)); do if [ `pgrep -u "$tomcatUser" "$tomcatExt" | wc -l` = 0 ]; then break fi for i in `pgrep -u "$tomcatUser" "$tomcatExt"`; do echo `date +"%Y/%m/%d %H:%M:%S"`" Cleanup Tomcat service!" >> "$tomcatLog" kill "$i" done sleep 1 done for ((;;)); do if [ `systemctl status tomcat.service | grep -i "Active:" | grep -i "Active: active (running)" | wc -l` = 1 ]; then break fi echo `date +"%Y/%m/%d %H:%M:%S"`" Start Tomcat service!" >> "$tomcatLog" /usr/bin/systemctl start tomcat.service sleep 1 done exit
2.2 创建服务触发
crontab -e
加入如下配置,
*/5 * * * * sh ~/scripts/autoTomcatManager.sh
没有评论