Shell script to notify of service down

Hi All

I am trying to write a shell script that will notify via email if a particular service is down. What I have so far is a script in

cron

like his:

#!/bin/sh
cd /usr/jdk/instances/jdk1.6.0/bin/sparcv9
jps -m

And the output of the above is

 81529 Jps
  52988 TaskControllerService
  52670 StreamControllerService
  

But what I would like to receive is an email with the following heading:

"Service name" "Value"

where

"Value"

is either

online

or

offline

.

Here is one way of doing it:

#!/bin/ksh

for service in TaskControllerService StreamControllerService
do
        if /usr/jdk/instances/jdk1.6.0/bin/sparcv9/jps | /usr/sfw/bin/ggrep -q "$service"
        then
                print "$service online"
        else
                print "$service offline"
        fi
done > mail_body

mailx -s "Subject" user@domain.com < mail_body

If you don't have ggrep replace that line with:

if /usr/jdk/latest/bin/sparcv9/jps | grep "$service" 1>/dev/null 2>/dev/null
1 Like

You don't need the GNU utilities version of grep to use the -q option. It has been in the formal standards for more than two decades and in the SVID and X/Open Portability Guides quite a while before there were formal standards for UNIX and UNIX-like system utilities.

Don, the grep in /usr/bin/ on Solaris does not support -q option: grep manual

/usr/bin/grep [-bchilnsvw] limited-regular-expression
	    [filename]...

It is there in /usr/xpg4/bin/grep and /usr/xpg6/bin/grep. In theory, -q support should have been added to /usr/bin/grep when /usr/xpg4/bin/grep was created (since adding that option would not break any correct use of grep before that option was added), but that didn't always happen.

Hi

It worked fine, after changing the

if

statement to avoid the

ggrep

.
Thank you very much