2012-12-04 49 views
0

我正在Tomcat服务器上运行Web应用程序。服务器代码中存在一个难以检测到的问题,导致它每天崩溃一次或两次。有空的时候,我会深入地纠正它。但直到那一天,在重新启动tomcat(/etc/init.d/tomcat7 restart)的问题情况下,或者基本上重新启动计算机,现在看来还是相当不错的解决方案。我想用wget而不是grep或其他东西来检测服务器的活力,因为即使tomcat正在运行我的服务可能会失败。如果Web服务(tomcat7)没有响应,重新启动

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. 

或只是stucks说

--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... 

否则后。你能否给我提供一个cron作业的shell脚本或其他的东西来做到这一点。如果有替代方案,我宁愿不使用cron。

回答

0

为什么不使用cron的呢?无论如何,igogo为tomcat + watchdog,并发现以下blog post

应该给你和想法如何解决你的问题。

心连心

0

OK我找到解决下/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 
done 

,其中最后一个查询检查表,看是否有任何最近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正在运行,但在最后的输出,从而更准确地检查这个检查它的。