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

use*_*236 2 apache bash 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
Run Code Online (Sandbox Code Playgroud)

我正在运行它 Ubuntu 14.04.2 LTS

另外,我如何更改脚本以测试安装在另一台机器上的 apache 服务器。请帮忙

Jul*_*Smz 8

这是 bash 脚本的工作示例,它检查 apache 状态,如果关闭则自动重新启动,并通过 unicode 表情符号中的 telegram 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
Run Code Online (Sandbox Code Playgroud)


Mar*_*ira 5

用这个:

service apache2 status
Run Code Online (Sandbox Code Playgroud)

或这个:

service --status-all | grep apache2
Run Code Online (Sandbox Code Playgroud)