This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

script to email if tomcat has stopped

 
Barbara Norway
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Does anyone have a shell script that checks that tomcat is running and emails if not?

I plan to have cron check at some interval and then mail me if it's down. The part I'm looking for help with is the body of the script.


ps -ef | grep tomcat | wc -l
if {whatever var for wc is}< 2
mail datamaven@anyschool.edu
fi (or endif)


I have heard that when grepping the ps command it is possible to get the grep statement itself as well as the statement one is looking for, but not always. So sometimes the wc will be one or two if tomcat is up. If it's down it could also be one (or zero). So that's the first question I have. The second is: what would the Bourne shell syntax be to accomplish the task?

Thanks in advance for any help.
Barbara aka farmkitty
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replace your line:

with

grep -v searches for lines, which don't contain a pattern (inVert): pattern 'grep' in this case, so it's filtered out.
grep -c counts the occurences, instead of displaying the match.
grep -q quiet, no output.

Finally we don't need the -c for grep, because it returns true or false.

&& needs no comment to javaian?

The 'mail to@some.one' insn't tested, and as far as I know, you have to use 'mailx' but I don't know the whole syntax - I guess you need a subject, etc. too.
[ June 07, 2004: Message edited by: Stefan Wagner ]
 
Barbara Norway
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Stefan,
Thanks! I tried it and this helps a lot. In case anyone is using Solaris 8 like I am, I had to use the /usr/xpg4/bin version of grep in order to use the -v flag:
ps -ef | grep tomcat | /usr/xpg4/bin/grep -vqc && mail kitty@catcity.edu

Barbara
 
Barbara Norway
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops...I meant to use the -q flag I needed to use the /usr/xpg4/bin version of grep.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want something more industrial-strength, try Big Brother. It can monitor your server port(s) and display status of multiple services and multiple hosts on a "big board" with drilldown capabilities.
 
Barbara Norway
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I love big brother ;-) we used it at a place i worked a couple of years ago. I'll have to make the time to install and configure it here!
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic