Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How could I monitor a service port from a bash shell?

I want to monitor a Java service (once per minute on port 9090) and then call "/etc/init.d/myservice -restart" if the service isn't responding with a simple HTML message.

How would you do something like this?

My idea was to use something similar to this:

wget -O - --no-check-certificate --progress=dot https://localhost:9090

Or

curl --insecure https://localhost:9090
share|improve this question
    
you're sending the output (-o) to /dev/null, how can you print any page content? You're probably print the verbose output of curl. If you want the content of the page try to remove --verbose and -vs and -o /dev/null and leave only --insecure –  coredump Apr 4 '11 at 2:57
    
Please use appropriate tags, such as one identifying the OS. –  John Gardeniers Apr 4 '11 at 2:59
    
@coredump - yep, your right, that shows the page content. –  djangofan Apr 4 '11 at 17:27
add comment

2 Answers

Use monit, it's more secure than writing a shell script for that.

If you are really want to write a script, use curl to get the content, grep it and restart the service in case of fail.

share|improve this answer
    
curl is a good idea. ill look into that. –  djangofan Apr 4 '11 at 1:55
    
Very often when wget is mentioned you post an answer suggesting to use curl instead, yet I've never seen you explain why. Care to enlighten me? –  John Gardeniers Apr 4 '11 at 3:04
4  
Wget is a tool to download stuff over http and ftp. curl and` libcurl` are swiss knifes for downloading, checking/downloading headers, cookies, injecting headers and cookies to test sites, changing user agents, it's very configurable and you can configure almost every aspect to perform a lot of tests that you can not even think of doing with a downloader tool like wget. It's not that wget is bad, it is great, it's just not the right tool for some jobs. –  coredump Apr 4 '11 at 3:15
add comment
up vote 0 down vote accepted

Thanks "coredump" . You gave me the hint I needed to solve the issue. I'll use a combination of CURL and also the answer on this page .

Here is what I came up with, which seems to work for me. If you can improve this answer, I might award the points to you.

#!/bin/bash
rm -f listening.htm
curl -s --connect-timeout 10 --insecure $1 > listening.htm
RETVAL=$?
echo "Curl return value : $RETVAL"
if [ $RETVAL -eq 2 ]; then
  echo "Missing URL parameter. Add URL and try again."
fi
if [ $RETVAL -eq 6 ]; then
  echo "Unable to resolve host. Check URL and try again."
fi
if [ $RETVAL -eq 0 ]; then
  echo "Is the site listening...?"
elif [ $RETVAL -eq 7 ]; then
  echo "Server timeout."
elif [ $RETVAL -eq 22 ];then
  echo "HTTP error above 400"
else
  rm listening.htm
  exit 1
fi

if grep -Fq "The site is listening..." listening.htm
then
  echo "Health is ok."
else
  echo "Service didn't respond.  Stopping."
  /home/ec2-user/SITE/stopService.sh
  sleep 6 
  echo "Starting service."
  /home/ec2-user/SITE/startService.sh
  sleep 10
  echo "Restarted at `date`" >> monitor.log
  rm listening.htm
fi
exit 1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.