1

我写了一些 shell bash 来检查 apache 是否有错误?我使用curl我的一页,如果head返回200 ok,似乎apache运行良好,否则,做 service httpd restart,为了监控脚本,我将一些运行日期和状态写入一些日志(1.txt)。

apache.sh

#!/bin/bash
curl -I http://www.mydomain.com/check.php 2>/dev/null | head -1 | grep -q " 200 OK"
if [ $? -eq 0 ]; then echo "ok $(date)" >> 1.txt; else service httpd restart;echo "restart $(date)" >> 1.txt; fi

crontab

*/6 * * * * /bin/sh /home/username/apache.sh >/dev/null 2>&1

但是昨天,我的apache还是没了,(子进程xxxx还是没有退出,发送了SIGKILL)。

我在 ssh 命令行中尝试过/bin/sh /home/username/apache.sh,没有写入日志(1.txt 为空),没有错误警告。

问题出在哪里?谢谢。

4

2 回答 2

1

您可以尝试一个更简单的脚本,如下所示:

rc=$(curl -Is -w '%{http_code}' -o /dev/null http://www.mydomain.com/check.php)
if [ $rc -eq 200 ]; then
    echo "ok $(date)" >> 1.txt
else
    service httpd restart
    echo "restart $(date)" >> 1.txt
fi
于 2013-10-12T10:33:44.383 回答
0

The service httpd restart will exit even when some of the child processes are still running - hence the msg child process xxxx still did not exit, sending a SIGKILL.
I have noticed this before, with db heavy websites (drupal, wordpress etc) and the solution is to stop apache, check if there are still child procs about and kill them, then restart apache.

service httpd stop
iter=0
# wait 30 seconds for any child procs to exit
while pgrep httpd
do
    iter=$(( iter + 1 ))
    if [ $iter -gt 6 ]
    then
         break
    fi
    sleep 5
done
# ok, I've finished waiting I'm going to kill 'em all
if pgrep httpd
then
    pkill -9 httpd
fi
service httpd start
于 2013-10-12T10:35:42.467 回答