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

时间:2016-04-13 06:49:14

标签: bash apache shell

我是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

我在Ubuntu 14.04.2 LTS

上运行它

此外,如何更改脚本,以便测试另一台计算机上安装的apache服务器。 请帮助

7 个答案:

答案 0 :(得分:4)

使用此:

service apache2 status

或者这个:

service --status-all | grep apache2

答案 1 :(得分:0)

而不是httpd尝试grep" apache2 "。确保尝试使用下一个命令检查服务并确定apache webserver的注册名称: 服务--status-all

答案 2 :(得分:0)

## 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
----------------------------------------------

答案 3 :(得分:0)

这在旧的Debian中非常完美。记住要使用bash而不是sh。

在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

答案 4 :(得分:0)

这是bash脚本的工作示例,该脚本检查apache的状态,如果apache处于关闭状态,则自动重新启动,并在Unicode表情符号内通过电报bot发出警报。

#!/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

答案 5 :(得分:0)

这是菜单驱动的一站式外壳脚本,您可以在其中检查防火墙,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

答案 6 :(得分:0)

试试看——这里最简单,最有教益,并且在 Ubuntu 20.04 上运行良好:

<块引用>
  1. 捕获状态输出到 bash 变量
  2. “if”状态包括子字符串(来自“Active:”语句) - 做你想做的工作
  3. "else" - 做你定义的另一项工作
#!/bin/bash
servstat=$(service apache2 status)

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