1833712 Members
2143 Online
110063 Solutions
New Discussion

Re: script help

 
SOLVED
Go to solution
Michael_33
Regular Advisor

script help

Hi All,

There are 2 java process running on my server.
I want to monitor them:

# ps -ef|grep java
root 20222 1 0 11:38:40 pts/0 0:26 /usr/java1.3.1/bin/../bin/sparc/n
ative_threads/java -classpath /usr/java1.3.1/l
root 20293 20187 0 12:55:05 pts/0 0:00 grep java
root 20173 1 0 11:37:30 pts/0 1:26 /usr/java1.3.1/bin/../bin/sparc/n
ative_threads/java -DproxySet=true -DproxyHost

If any of the 2 java was down, then email to
abc@123.com

Any idea?
Thanks
11 REPLIES 11
Steven E. Protter
Exalted Contributor

Re: script help

Basic structure

ps -ef | grep java > /tmp/javamon

# You may need something more unique than java, but you'll have to tweak it.

javaproc=`wc -l` /tmp/javamon

if [ javaproc le 3 ] then
mailx -s "java process problem" abc@123.com
fi

there are a lot of better script writers out there. I am certain someone will top me.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Fragon
Trusted Contributor

Re: script help

Hi,
That's easy if you have your Sendmail configured OK!

if ps -ef|grep -v $$|grep "java"
then
echo ""
else
mailx -s "At least one java service is down" abc@123.com fi

Have a good day!
-ux
Michael_33
Regular Advisor

Re: script help

thanks!
Con O'Kelly
Honored Contributor

Re: script help

#!/usr/bin/sh

PROC_CHK=$(ps -ef | grep [j]ava | wc -l)

if [ $PROC_CHK -ne 2 ]
then
echo "Jave Processes Running: $PROC_CHK" | mailx -s "Jave Process Problem" abc@123.com
fi

Cheers
Con
Michael_33
Regular Advisor

Re: script help

thanks!
I will try it later,pls reply so I can give you
10 points if it works. :)
Fragon
Trusted Contributor

Re: script help

Sorry,a little bug, it's a new one:

#!/usr/bin/sh

pjavan=ps -ef|grep -v $$|grep -i java |wc -l

if [ $pjavan -ln 2 ]
then
echo "Some msg you want!"|mailx -s "At least one Java process down!" abc@123.com
fi

-ux
F. X. de Montgolfier
Valued Contributor

Re: script help

Hi,

#!/usr/bin/ksh
if [ `ps -ef|grep -v grep|grep -c java` -lt 2 ]
then
/usr/bin/mailx -s "java process down" abc@123.com
fi

Not that it's so different from the other solutions you've had, but people always forget to use the "grep -c" command...

FiX
Michael_33
Regular Advisor

Re: script help

Hi FiX,

when I run your script, it seems hang,
I can not get the mail. any idea?
Kenneth_19
Trusted Contributor
Solution

Re: script help

You should include a filename in the last part of the mailx command as the body of the mail:

/usr/bin/mailx -s "java process down" abc@mail.com < filename, if you want a plain body, simply replace the filename with /dev/null.

Kenneth
Always take care of your dearest before it is too late
F. X. de Montgolfier
Valued Contributor

Re: script help

Michael,

yes, I have an idea: I'm stoopid ;-P
You need to have a body for the mailx command.

the correct script is:

#!/usr/bin/ksh
if [ `ps -ef|grep -v grep|grep -c java` -lt 2 ]
then
echo "this is what I forgot in the last message"|/usr/bin/mailx -s "java process down" abc@123.com
fi

Sorry, my bad...

FiX



Tommy Palo
Trusted Contributor

Re: script help

To check every 15:th minute

# crontab -e
0,15,30,45 * * * * expr $(ps -ef|grep java|grep -v grep|wc -l) != 2 >/dev/null && mail -s "Java proc. down" abc@123.com
Keep it simple