2

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
djangofan
  • 4,172
  • 10
  • 45
  • 59
  • 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 04 '11 at 02:57
  • Please use appropriate tags, such as one identifying the OS. – John Gardeniers Apr 04 '11 at 02:59
  • @coredump - yep, your right, that shows the page content. – djangofan Apr 04 '11 at 17:27

2 Answers2

6

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.

coredump
  • 12,573
  • 2
  • 34
  • 53
  • curl is a good idea. ill look into that. – djangofan Apr 04 '11 at 01: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 04 '11 at 03: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 04 '11 at 03:15
0

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
djangofan
  • 4,172
  • 10
  • 45
  • 59