【问题标题】:shell bash code not run with no error warningshell bash 代码无法运行且没有错误警告
【发布时间】:2013-10-20 09:29:42
【问题描述】:

我写了一些 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 为空)且没有错误警告。

问题出在哪里?谢谢。

【问题讨论】:

  • 检查您的 syserr 登录 /var/logs 并尝试提供 curl 的完整路径,例如 /usr/local/bin/curl 或任何您拥有的。

标签: linux apache bash shell curl


【解决方案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

【讨论】:

  • 第 1 行 ")syntax error: invalid arithmetic operator (error token is " 中的错误以及如何在不更改行的情况下编写 shell?我像你一样写一些格式,总是得到syntax error near unexpected token 'fi'谢谢。
  • 可能[[ 不可用或者您没有使用 BASH。立即尝试编辑的代码。
  • syntax error near unexpected token 'fi',我在dreamweaver cs4中编辑。
  • 奇怪,尝试运行curl -Is -w '%{http_code}' -o /dev/null http://www.mydomain.com/check.php 命令,看看你得到了什么输出。
  • 是的,这就是它应该做的。之后,您可以保留旧的if/else,但使用不同的条件$rc -eq 200
【解决方案2】:

即使某些子进程仍在运行,服务 httpd restart 也会退出 - 因此 msg 子进程 xxxx 仍然没有退出,发送 SIGKILL。
我之前注意到这一点,对于 db 重的网站(drupal、wordpress 等),解决方案是停止 apache,检查是否还有子进程并杀死它们,然后重新启动 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-29
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2020-03-28
    相关资源
    最近更新 更多