bash 脚本 : to check if Apache server is up and running

标签 bash apache shell

我是 bash 脚本的新手,我想弄清楚为什么下面的脚本输出 Apache 服务器未运行 而它运行正常。

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 服务器。 请帮忙

最佳答案

这是一个 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

关于bash 脚本 : to check if Apache server is up and running,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36590643/

相关文章:

html - 使用 grep 从本地文件中的 HTML 标记中获取文本

linux - 如何在 Linux 上转义单引号

java - 更改 Apache CXF 中特定请求的客户端超时

perl - 如何编写CGI脚本以通过ssh执行远程脚本并显示来自远程执行的数据?

apache - 公共(public) IP 网页 "Connection Timed Out"连接时

shell - ${arr[@]} 在 sed 命令中

linux - bash 中存在哪些注入(inject)漏洞(如果有的话),我该如何防范它们?

linux - 从终端删除多个文件

macos - 如何在 mac os 中的 iTerm 中删除单词

linux - 检测 linux 版本 5 或 6