简体   繁体   English

Shell bash代码无法运行且没有错误警告

[英]shell bash code not run with no error warning

I write some shell bash to check if apache has wrong? 我写一些shell bash来检查apache是​​否有错误? I use curl my one page, if head return 200 ok , it seems apache run well, else, do service httpd restart , To monitor whether the script, i write some the run date and status into some log(1.txt). 我使用curl一页,如果返回200 ok ,似乎apache运行良好,否则,请执行service httpd restart ,以监视脚本是否运行,我将一些运行日期和状态写入了一些log(1.txt)中。

apache.sh 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 crontab中

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

But yesterday, my apache still gone, (child process xxxx still did not exit, sending a SIGKILL). 但是昨天,我的Apache仍然消失了(子进程xxxx仍然没有退出,发送了SIGKILL)。

I tried in ssh command line /bin/sh /home/username/apache.sh , no log has been writing(1.txt is empty) with no error warning. 我在ssh命令行/bin/sh /home/username/apache.sh尝试过,没有日志在写(1.txt为空),没有错误警告。

Where is the problem? 问题出在哪儿? thanks. 谢谢。

You can try a simpler script like this: 您可以尝试一个更简单的脚本,如下所示:

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

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. 即使某些子进程仍在运行,httpd重新启动服务也会退出-因此,味精子进程xxxx仍未退出,发送了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. 我以前在数据库密集型网站(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM