尝试使脚本检查apache状态

时间:2011-09-21 08:47:00

标签: bash

所以这是我的bash脚本。

APACHECHECK="$which telnet localhost 80"

echo -e "\n Checking APACHE\n"

$($APACHECHECK)

#if [[ $($APACHECHECK) == *Connected* ]]; then
#
#       echo -e "Apache is running on port 80 :)"
#               else
#                       echo -e "\n Apache is down!"
#               fi
#

忽略评论。该脚本运行telnet localhost 80。如果它返回“已连接”,则它会报告apache正在运行。

问题是脚本没有完全运行。这是我的脚本输出:

root@vr6 [~]# ./megatool 

 Checking APACHE

telnet: connect to address ::1: Connection refused

这是我运行telnet localhost 80

root@vr6 [~]# telnet localhost 80
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

此外,我想抑制输出,只是让我的脚本报告apache的回显正在运行,或者它不像我的if if语句。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

为什么不使用apachectl?它带有apache并允许你这样做:

apachectl status

答案 1 :(得分:2)

帮助telnet解决方案:

if telnet localhost 80 </dev/null 2>&1 | grep -q Connected; then
  echo "Connected"
else
  echo "no connection"
fi