我在Tomcat服务器上运行web应用程序。服务器代码中有一个难以检测的问题,导致它每天崩溃一到两次。当我有时间的时候,我会深入研究并纠正它。但直到那一天,在有问题的情况下,重新启动tomcat (/etc/init.d/tomcat7 restart)或基本上重新启动机器目前看起来也是很好的解决方案。我想用wget而不是grep或其他东西来检测服务器的活跃性,因为即使tomcat正在运行,我的服务也可能会关闭。
wget localhost:8080/MyService/输出
--2012-12-04 14:10:20-- http://localhost:8080/MyService/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2777 (2.7K) [text/html]
Saving to: “index.html.3”
100%[======================================>] 2,777 --.-K/s in 0s
2012-12-04 14:10:20 (223 MB/s) - “index.html.3” saved [2777/2777]当我的服务结束的时候。和输出
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:8080... failed: Connection refused.或者只是在说了一句话后感到厌烦
--2012-12-04 14:07:34-- http://localhost:8080/MyService/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response...不然的话。你能提供我一个shell脚本与cron作业或其他事情来做这件事。如果有其他选择,我不喜欢使用cron。
发布于 2012-12-04 23:25:28
发布于 2012-12-05 02:51:42
好的,我找到了解决方案,在/etc/rc5.d/下添加一个脚本,就像这里所说的http://www.linuxforums.org/forum/mandriva-linux/27687-how-make-command-run-startup.html
!/bin/bash
echo "Restarted: " `date` >> /home/ec2-user/reboot.log
sleep 600
while [ 1 ]
do
var=`php -f /home/ec2-user/lastEntry.php`
if [ ${#var} -gt 3 ]
then
echo "Restarting: " `date` >> /home/ec2-user/reboot.log
reboot
fi
sleep 60
donewhere last查询检查一个表,查看在过去10分钟内是否有任何条目。
<?php
mysql_connect('ip', 'uname', 'pass') or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$query="SELECT min(now()-time) as last FROM table;";
$result = mysql_query($query)or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['last'];
?>有更直接的方法来检查tomcat是否正在运行,但是这个检查最后一次输出,所以更准确地检查它。
https://stackoverflow.com/questions/13705083
复制相似问题