开发者

find httpd running or not using /etc/init.d/httpd inside script

I need use /etc/init.d/httpd status command to verify whether its running or not inside shell. I don't want to use pidof pgrep etc. --something like

retval=`/etc/init.d/httpd status`
if [ $retval -eq "running" ];then echo "ye开发者_运维问答s" ; else echo "no";fi

Any thoughts ?


$? will give you the value of the most recent return code. E.g.

/etc/init.d/httpd status > /dev/null # ignore stdout
if [ $? -eq 0 ]; then 
    echo "yes"
else 
    echo "no"
fi

For more details, see http://tldp.org/LDP/abs/html/exit-status.html


You can run your command directly in the if statement:

if /etc/init.d/httpd status > /dev/null
then

Or you can check the output string:

retval=$(/etc/init.d/httpd status)
if [[ $retval == *running* ]]
then

By the way, -eq is a numeric test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜