1

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 "yes" ; else echo "no";fi

Any thoughts ?

2 Answers 2

3

$? 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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.