0

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

否则。你能给我一个带有 cron 作业的 shell 脚本或其他东西来做这件事吗?如果有替代方案,我宁愿不使用 cron。

4

2 回答 2

0

为什么不使用 cron 呢?无论如何,ig 搜索tomcat + watchdog并找到了以下博客文章

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

hth

于 2012-12-04T15:25:27.657 回答
0

好的,我找到了在 /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

where last query 检查表以查看过去 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 是否正在运行,但这会检查最后一个输出,因此可以更准确地检查它。

于 2012-12-04T18:51:41.683 回答