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

zte4gxcn  于 2022-11-16  发布在  Apache
关注(0)|答案(7)|浏览(133)

我是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服务器.

xeufq47z

xeufq47z1#

使用此选项:

service apache2 status

或者这样:

service --status-all | grep apache2
zqry0prt

zqry0prt2#

这是一个bash脚本的工作示例,该脚本检查apache状态,如果关闭则自动重新启动,并通过unicode emoji中的telegraph 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
dhxwm5r4

dhxwm5r43#

试试看吧--这里最简单、最有启发性,而且在Ubuntu 20.04上运行良好:
1.捕获状态到bash变量的输出
1.“if”状态包括子字符串(来自“Active:“语句)-执行您想要的作业
1.“else”-执行您定义的另一项工作

#!/bin/bash
servstat=$(service apache2 status)

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

92dk7w1h4#

这在旧的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
x3naxklr

x3naxklr5#

尝试grep“apache 2"而不是httpd。为了确保正确,尝试使用下一个命令检查服务,并确定apache Web服务器的注册名称:服务--状态-全部

fzsnzjdm

fzsnzjdm6#

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

mutmk8jj7#

这是菜单驱动的一个停止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

相关问题