2014-04-24 116 views

回答

1

您没有在远程主机上运行命令。

试试这个。

if ssh -qn [email protected] ps aux | grep -q httpd; then 
    echo "Apache is running" 
else 
    echo "Apache is not running" 
fi 

只要是明确的,ps aux是参数ssh,因此这是被远程主机上执行了什么。 grep作为本地脚本的子节点运行。

+1

同意与sudoer用户运行。另外,为了检查服务是否正在运行,我宁愿使用'/etc/init.d/httpd status | grep pid'而不是'ps aux | grep -q httpd' – yejinxin

0

首先,httpd在ubuntu中不可用。对于Ubuntu的Apache2是可用的。

所以这个命令ps aux | grep [h]ttpd将无法​​在Ubuntu的工作。

无需编写任何脚本来检查Apache的状态。从ubuntu的终端运行此命令,以获得状态:

sudo service apache2 status 

输出将是:

A>如果Apache运行:Apache2 is running (pid 1234)

B>如果Apache没有运行:Apache2 is NOT running.

0

由于ssh以远程命令的退出状态返回检查ssh的手册页并搜索退出状态

所以它的那样简单

ssh [email protected] "/etc/init.d/apache2 status" 
if [ $? -ne 0 ]; then      # if service is running exit status is 0 for "/etc/init.d/apache2 status" 
echo "Apache is not running" 
else 
echo "Apache is running" 
fi 

你不需要PS或者grep的这个

+0

显式检查'$?'是一个反模式。 “if”和朋友的目的恰恰是运行命令并检查其退出状态。写这个的惯用方式就是'if ssh root @ ip“/etc/init.d/apache2 status”;那么......(这里的引用实际上是可选的)。 – tripleee

2

尝试以下操作:

if ssh -qn [email protected] pidof httpd &>/dev/null ; then 
    echo "Apache is running"; 
    exit 0; 
else 
    echo "Apache is not running"; 
    exit 1; 
fi 

这些exit命令将发送正确的EXIT_SUCCESSEXIT_FAILURE(如果需要,将来可以使用此扩展脚本)。

只有一个忠告:最好把脚本作为远程过程通过SSH账号

相关问题