script didn;t work in cron !!! @_@

Hi all,

I am writing a script to monitor some processes existence in the system. It works perfectly by running the script manually in commend line. However, when I put it under cron to run it failed. Everything time when the variable is null in the if statment. it failed and quitted. Here is part of my script.

****
NTP_STATUS=`ps -ef | grep xntp | grep -v grep`
if [ -z $NTP_STATUS ]
then
echo "WARNING - NTP daemon is not running"
else
echo "MESSAGE - NTP daemon is running"
fi
****

Therefore, when the variable NTP_STATUS is null, the script dropped out and quitted.

The weird thing is that it works fine when I run an script manually, but fails in cron....

please help guys. Thanks in advance.

just change the following

i hope there is only one daemon process that you are looking for...
then

NTP_STATUS=`ps -ef | grep xntp | grep -v grep|wc -l`
if [ $NTP_STATUS < 1 ]
then
echo "WARNING - NTP daemon is not running"
else
echo "MESSAGE - NTP daemon is running"
fi

I hope this solves the problem.

Cheers
Nimish

The original if statement will work only if xntp is running. To make it work if xntp is not running use quotes:
if [ -z "$NTP_STATUS" ]

The second if statement is wrong. < is for file redirection. You need a numeric compare anyway. So use -lt:
if [ $NTP_STATUS -lt 1 ]

You do not tell us what system or what shell you are using. So take the appropriate steps to ensure that cron will use the proper shell. If cron runs those commands with a different shell than the one you tested with, it may or may not work.