博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to automatically restart Apache Tomcat when...
阅读量:6280 次
发布时间:2019-06-22

本文共 2345 字,大约阅读时间需要 7 分钟。

  hot3.png

This short blog is about monitoring and restarting Tomcat through crontab if and when it silently dies for whatever cause. The process described here works and was tested in our Tomcat 7 installation in Debian Squeeze servers.

Below is the entire script.

1
2
3
4
#!/bin/sh
 
/bin/netstat
-
ln
|
/bin/grep
":8080 "
|
/usr/bin/wc
-l | \
     
/usr/bin/awk
'{if ($1 == 0) system("/etc/init.d/tomcat start") }'

Let us breakdown the commands of our script above.

1
/bin/netstat
-
ln

netstat is a program to list network connections. The option -l tells netstat to print only listening sockets. -n commands netstat to list addresses and ports and not to resolve hosts.

1
/bin/grep
":8080 "

grep searches lines of input for match against a pattern from an input file or standard input. In our case, it searches for a match for ":8080 " from the output of netstat taken from standard input. Our Tomcat process listens to port 8080. If you used other port for your Tomcat installation, you may want to change this to match the port you are using.

1
/usr/bin/wc
-l

wc counts the number of lines from an input. In our case, the output from grep. This is accomplished with the -l option.

1
/usr/bin/awk
'{if ($1 == 0) system("/etc/init.d/tomcat start") }'

And finally we create simple awk program to start Tomcat if wc passes 0 to it, which is 0 number of lines counted by wc from its input.

Save the script giving it any desired name and to desired location. For the sake of this blog, we name our script tomcat-start and put it in /opt/scripts. Change the access permission to the newly saved file to include execute attribute.

1
chmod
+x tomcat-start

To learn more about the command above, please visit the documentation of each command or visit its man pages.

Crontab

Our script is done. Now we ask crontab to run it at a defined interval. Here crontab checks our Tomcat process every 5 minutes.

Run crontab -e to open the crontab editor and then enter the line below and save.

1
2
*
/5
* * * *
/opt/scripts/tomcat-start
/dev/null 
\
     
>>
/opt/scripts/tomcat-start-log
.txt

Our crontab entry above says run tomcat-start every 5 minutes as already mentioned and save the screen output of Tomcat to to our log file, tomcat-start-log.txt. You may want to change the 5-minute interval to suit your needs.

That's it. Good luck.

转载于:https://my.oschina.net/doz/blog/57157

你可能感兴趣的文章
/etc/fstab,/etc/mtab,和 /proc/mounts
查看>>
Apache kafka 简介
查看>>
socket通信Demo
查看>>
技术人员的焦虑
查看>>
js 判断整数
查看>>
建设网站应该考虑哪些因素
查看>>
mongodb $exists
查看>>
js实现页面跳转的几种方式
查看>>
sbt笔记一 hello-sbt
查看>>
常用链接
查看>>
pitfall override private method
查看>>
!important 和 * ----hack
查看>>
聊天界面图文混排
查看>>
控件的拖动
查看>>
svn eclipse unable to load default svn client的解决办法
查看>>
Android.mk 文件语法详解
查看>>
QT liunx 工具下载
查看>>
内核源码树
查看>>
Java 5 特性 Instrumentation 实践
查看>>
AppScan使用
查看>>