【问题标题】:A shell script that checks if the nginx service is running, with action一个检查 nginx 服务是否正在运行的 shell 脚本,带有操作
【发布时间】:2018-08-17 22:12:56
【问题描述】:
#!/bin/bash
date=$(date +"%Y-%m-%d %H:%M:%S")
getpid=$(pgrep nginx | wc -l 
if [ "$getpid" > 0 ]
then
  echo 'Nginx is Fine, It is Running at' $date
else
  echo "Error on Nginx and stoped at" $date
  sudo fuser -k 443/tcp
  sudo service nginx start
  sudo service  monit start
  sudo monit monitor all
  echo "Error on Nginx and stoped at" $date | mailx -s "The Nginx Stop - But it is Fixed" -A /root/nginx_log3.txt mymail.com
fi
exit 0;

我看到了几种检查服务的方法:

$(ps ax | grep myName | fgrep -v grep | awk '{ print $1 }')

if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))

还有几种方法...

检查进程是否正在运行的无数方法,但问题是,即使我终止nginx,任何使用 PS 的命令总是会带来许多带有pid 值的结果!因为pgrep 在我停止服务 nginx 后总是显示 0,而gprep 似乎很适合与小于任何 pid 的值 0 进行比较,

因为如果它已经运行,总是显示 pid 高于 velor 0,pgrep 是路径,但问题是我有一个 crontab 每 3 分钟运行一次这个脚本,即使进程正在运行, pid大于零,正在重启服务!

我发现几个博客教授了几个脚本,但没有一个有效!

我有monit 来保持服务活动,但有时它会失败。

显然,我不确定如何将提取的 pid 的值与值 0 进行比较:

getpid=$(pgrep nginx | wc -l)
    if [ "$getpid" > 0 ]

因为服务总是每 3 分钟重启一次,即使服务以 pid 运行(值大于 0)运行也是如此

非常感谢您的帮助!

【问题讨论】:

  • 你的初始化系统没有检查服务状态的方法吗?

标签: bash shell cron


【解决方案1】:

我可能会检查您尝试提供的 www 服务,而不是该服务是否正在运行。看看这些不同的例子:

    https=$(nc -z localhost 443)
    http=$(nc -z localhost 80)
    netstt_cnt=$(netstat -ntlp | grep httpd | wc -l)

    http_issues=""
    if [[ ! $https ]] || [[ ! $http ]] ;then
        http_issues=" -Http/https ports not detected "
    fi
    if [ $netstt_cnt -ne 2 ] ;then
        http_issues="${http_issues} -Netstat not reporting httpd "
    fi

    # -- if http_host_check is set perform httpd checks
    local code_stat=""
    if [[ $http_host_check ]] ;then
        http_code=$(curl --write-out %{http_code} --silent --output /dev/null $http_host_check)
        if [ $http_code -lt 1 ] ;then
            http_issues="${http_issues} -Apache NOT serving pages http_code=$http_code. "
        elif [ $http_code -gt 399 ] ;then
            http_issues="${http_issues} -Apache Error http_code=$http_code on test page ${http_host_check}. "
        fi
        code_stat=", (http_code=${http_code}) "
    fi

    # -- php FPM sock, see readonly var $PHP_FPM_SOCK for use set to "" to disable this check.
    if [[ $PHP_FPM_SOCK ]] ;then
        if ! echo /dev/null | socat UNIX:${PHP_FPM_SOCK} - ;then
            http_issues="${http_issues} -php-fpm sock not communicating"
        fi
    fi

    if [[ $http_issues ]] ;then
            echo "Error on Nginx and stoped at" $date
            sudo service nginx stop
            sudo service  monit stop
            sudo fuser -k 443/tcp
            sleep 10    
            sudo service nginx start
            sudo service  monit start
            sudo monit monitor all
            if [[ $http_host_check ]] ;then
                http_code=$(curl --write-out %{http_code} --silent --output /dev/null $http_host_check)
             if [ $http_code -lt 1 ] ;then
                http_issues="${http_issues} -Apache NOT serving pages http_code=$http_code. "
            elif [ $http_code -gt 399 ] ;then
                http_issues="${http_issues} -Apache Error http_code=$http_code on test page ${http_host_check}. "
            fi
            sleep 5
            code_stat=", (http_code=${http_code}) "
            echo "Webserver had a problem, current status is $code_stat" $date | mailx -s "The Nginx stop: $code_stat" -A /root/nginx_log3.txt mymail.com  
    fi

    echo "Current Status : $http_issues"

更新:这里添加了超级简单的示例:

http_code=$(curl --write-out %{http_code} --silent --output /dev/null http://my_domain.com/)
    if [ $http_code -lt 1 ] ;then
        echo "${http_issues} -Apache NOT serving pages http_code=$http_code. "
        // ... do something here (restart web server)..
    fi

【讨论】:

  • 嗨@Mike_Q 在我的测试中我无法让它工作,你的代码远远高于我的简单建议,只是为了将 pid 的值提取为与 0 相当的格式,我的代码我知道我可以做几件事,比如 mysql , php-fpm ,但我无法让你的代码与 nginx 一起工作。我停止了服务,但它并没有像我的代码一样重新启动 nginx。
  • @Mike_Q 你给了我一个很好的主意!!我可以放置一个文件 754fsfsf73876gsfg574sfjaks485467sfs.php 以获得一个唯一的 URL,我可以通过脚本访问这个 url,我可以在其中一个站点中的所有这些字符的 php 中进行回显,并与存储的值进行匹配在脚本中,如果可以访问,则可以进行匹配,如果无法匹配,则一切正常,服务会在所有服务上启动。我会在堆栈上做一个 Aks!
  • @ArianeMartinsGomesDoRego 我可以提供一个更基本的示例,但您需要做的就是检查 http_code 字符串是否由某些检查设置。此外,您可能甚至不需要制作网页,因为我们正在检查 CURL 是否有效。例如: curl --write-out %{http_code} --silent --output /dev/null your_domain.com 如果您的域已关闭,将打印出 000。您不需要检查它要求的页面的内容。
  • @ArianeMartinsGomesDoRego 我在上面的帖子中添加了我正在谈论的内容的示例。
  • @Mike_Q 我了解到,对这个任务使用 PID 值是最糟糕的方法,ps、pgrep,任何使用 pid 值的东西都是最糟糕的选择。我做了很多工作因为正在使用不适当的资源。我刚刚在您的更新中用那个简单的代码创建了第五个代码。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2014-08-21
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多