bash 脚本:检查 Apache 服务器是否已启动并正在运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36590643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash script: to check if Apache server is up and running
提问by user4943236
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. Kindly help
另外,我如何更改脚本以测试安装在另一台机器上的 apache 服务器。请帮忙
回答by Marcelo ávila de Oliveira
Use this:
用这个:
service apache2 status
Or this:
或这个:
service --status-all | grep apache2
回答by JuliSmz
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
回答by nix
Instead of httpd try to 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
而不是 httpd 尝试 grep “ apache2”。确保尝试使用下一个命令检查服务并确定 apache 网络服务器的注册名称: service --status-all
回答by shrikrishna sharma
## 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
----------------------------------------------
回答by JuliSmz
This work perfect in an old Debian. Remember to run with bash and not with sh.
这项工作在旧的 Debian 中非常完美。请记住使用 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