Service script

Hi Folks,

I need to find out and trigger if the service is running to OK and not running to NOTOK. I use the below script but, its not the precise output. Anything missed out in the script:

if chkconfig --list | grep -i sendmail
then
        echo " The sendmail service is not running : STATUS--> NOOK"
        echo
else
        echo " The sendmail service is running : STATUS--> OK"
        echo
fi

Use the service command itself to check if a service is running.

if service sendmail status >/dev/null; then
  echo OK
else
  echo NOK
fi

try changing if loop to check for count of sendmail if 0 found if loop will result in false else true

 
if [ `chkconfig --list | grep -c -i sendmail` ]

Thanks Vidya and Scott,

Actually, if the sendmail if on -> its should flag as OK
and if sendmail if off -> it should flag as NOTOK as per the command chkconfiig -list | grep -i sendmail

  chkconfig --list | grep -i sendmail
sendmail        0: off   1: off   2: on    3: on    4 :on    5: on    6: off
if chkconfig --list sendmail | grep '[345]:off'
then
  echo " sendmail not configured for autostart : STATUS--> NOOK"
  echo
else
  echo " sendmail configured for autostart : STATUS--> OK"
  echo
fi

Thanks Madein. Your script works perfectly. also one more thing.

chkconfig --list | grep -i telnet
    ekrb5-telnet:      off
    krb5-telnet:       off
    telnet:            off

How can I make sure like the previous script.

Make sure what?
All 3 services are on/off? Any of these services are on/off?

Hi MadeIn,

Want to make sure to "NOTOK" if any one is ON.

if chkconfig --list telnet | grep -w 'on'
then
  echo " telnet configured for autostart : STATUS--> NOOK"
  echo
else
  echo " telnet not configured for autostart : STATUS--> OK"
  echo
fi

Hi MadeIn,

I got the error while using the script like below:

if chkconfig --list telnet | grep -w 'on' 
then   echo " telnet configured for autostart : STATUS--> NOOK" 
  echo 
else 
  echo " telnet not configured for autostart : STATUS--> OK"   
echo 
fi
ERROR : error reading information on service telnet: No such file or directoryEven I tried with the below script: no luck.

if chkconfig --list  | grep -i telnet | grep -w 'on'
then
        echo "telnet is disabled: STATUS--> OK"
        echo
else
        echo "telnet is enabled: STATUS--> NOTOK"
        echo
fi
echo

My last post was mistaken; indeed there must be chkconfig --list | grep

if chkconfig --list | grep 'telnet.*[[:blank:]]on'
then
  echo " telnet configured for autostart : STATUS--> NOOK"
  echo
else
  echo " telnet not configured for autostart : STATUS--> OK"
  echo
fi