Help on Process Monitoring Script

Hi All,

I have a Java application running in the background which process looks like this.

 
java -DMyService=Y -DWorkingDir

And I have a monitoring script which looks like this;

 
count_service=`ps -aef | grep MyService | wc -l`
if [ count_service -lt 2 ] ; then 
echo "Service_Stopped on `date`" >> $servicelog
fi

Although the service keeps on running, sometimes, I got the message in my log that the service has stopped. Is there something wrong with my monitoring script? To my knowledge, it should be 2; one for Java and another is for grep.

Please help.

Thanks in advance,
SW

You should tell what OS you are running.

Under Solaris, here is a simpler and more robust solution:

if pgrep -f MyService >/dev/null
then 
  echo "Service_Stopped on `date`" >> $servicelog
fi
ps -aef | grep '[M]yService' >/dev/null || echo stopped

hi cfajohnson

why do you use "[M]" here?

what's the difference to

ps -aef | grep MyService >/dev/null || echo stopped

To eliminate the grep command from the output. The regex '[M]yService' does not match the string '[M]yService'.

thanks a lot, good to know, I used to write
grep -v grep :slight_smile:

Wow.... Thanks a lot for great replies :). So, that means my script is wrong... :frowning: