简体   繁体   English

bash 脚本:检查 Apache 服务器是否已启动并正在运行

[英]bash script: to check if Apache server is up and running

I am new to bash scripting and trying to figure out why the below script is outputting that Apache server is not running whereas it is running properly.我是 bash 脚本的新手,并试图弄清楚为什么下面的脚本输出Apache server is not running而它运行正常。

ps cax | grep httpd
if [ $? -eq 0 ]; then
 echo "Process is running."
else
 echo "Process is not running."
fi

I'm running it on Ubuntu 14.04.2 LTS我在Ubuntu 14.04.2 LTS上运行它

Also, how do I make changes to the script that this can test apache server installed on another machine.另外,我如何更改脚本以测试安装在另一台机器上的 apache 服务器。 Kindly help请帮助

Use this:使用这个:

service apache2 status

Or this:或者这个:

service --status-all | grep apache2

This is a working sample of bash script which check the apache status, restart it automatically if down, and alert by telegram bot within unicode emoji.这是一个 bash 脚本的工作示例,它检查 apache 状态,如果关闭则自动重新启动,并通过 unicode 表情符号中的电报机器人发出警报。

#!/bin/bash
telegram=(xxxxx, yyyyyy)

if ! pidof apache2 > /dev/null
then
    # web server down, restart the server
    echo "Server down"
    /etc/init.d/apache2 restart > /dev/null
    sleep 10

    #checking if apache restarted or not
    if pidof apache2 > /dev/null
    then
        for i in "${telegram[@]}"
        do
        curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restarted succesfully."
        done
    else
        for i in "${telegram[@]}"
        do
        curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restart failed. Please check manually."
        done
    fi
fi

Instead of httpd try to grep " apache2 ".而不是 httpd 尝试 grep “ apache2 ”。 To be sure try to check services with the next command and decide the registered name of the apache webserver: service --status-all确保尝试使用下一个命令检查服务并确定 apache 网络服务器的注册名称: service --status-all

## Plz run this script .. its working 
------------------------------------------------
ps cax | grep httpd
if [ $? -eq 1 ]
 then
 echo "Process is running."
else if [ $? -eq 0 ]
 echo "Process is not running."
fi
fi
----------------------------------------------

This work perfect in an old Debian.这项工作在旧的 Debian 中非常完美。 Remember to run with bash and not with sh.请记住使用 bash 而不是 sh 运行。

In Centos replace with httpd.在 Centos 中替换为 httpd。

#!/bin/bash
if [ $(/etc/init.d/apache2 status | grep -v grep | grep 'Apache2 is running' | wc -l) > 0 ]
then
 echo "Process is running."
else
  echo "Process is not running."
fi

This is menu driven one stop shell script in which you can check the firewall,apache or any other webservices ,you can start or stop the services just by choosing the option in below script这是菜单驱动的一站式 shell 脚本,您可以在其中检查防火墙、apache 或任何其他 Web 服务,只需选择以下脚本中的选项即可启动或停止服务

echo "welcome please select your options"
read choice
firewall=`sudo systemctl status firewalld`
apache=`sudo systemctl status apache2`
firewall1=`sudo systemctl stop firewalld`
apache1=`sudo systemctl stop apache2`
startrfirewall=`sudo systemctl start firewalld`
startapache=`sudo systemctl start apache2`

case $choice in
         1) status of the firewall is $firewall
                          ;;
         2) status of apache is $apache
                          ;;
         3) echo stop firewall by $firewall1
                          ;;
         4) echo stop apache by $apache1
                          ;;
         5) echo start firewall by $startrfirewall
                          ;;
         6) echo start apache by $startapache
                          ;;
         *) echo exit
esac

Try and see - simply simplest, most didactic here and well working on Ubuntu 20.04:试试看 - 在这里最简单,最有教益,并且在 Ubuntu 20.04 上运行良好:

  1. catching output of status to bash variable捕获状态输出到 bash 变量
  2. "if" status includes substring (from "Active:" statement) - do job you wanted “if”状态包括子字符串(来自“Active:”语句) - 做你想做的工作
  3. "else" - do another job you defined "else" - 做你定义的另一项工作
#!/bin/bash
servstat=$(service apache2 status)

if [[ $servstat == *"active (running)"* ]]; then
  echo "process is running"
else echo "process is not running"
fi

I put this together based on the above and made so can use other services.我根据以上将其组合在一起并制作了可以使用其他服务。

Hope this helps.希望这可以帮助。

#!/bin/bash
# Must be running as root or via sudo permissions to be able to restart
# Put your process name restart command line in
PROCESS_NAME=httpd
if ! pidof $PROCESS_NAME > /dev/null
then
    # web server down, restart the server
    echo "Server $PROCESS_NAME down"
    /usr/sbin/apachectl restart > /dev/null
    echo "Tried restart of $PROCESS_NAME. Waiting 10 seconds to settle."
    # wait ten
    sleep 10

    #checking if process restarted or not
    if pidof $PROCESS_NAME  > /dev/null
    then
        echo "$PROCESS_NAME was down but is now up."
    else
        echo "$PROCESS_NAME is still down. Please take some action."
    fi
else
    echo "Server $PROCESS_NAME up."
fi

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

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