Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

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
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
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.

rm -f listening.htm
curl -s --connect-timeout 10 --insecure $1 > listening.htm
echo "Curl return value : $RETVAL"
if [ $RETVAL -eq 2 ]; then
  echo "Missing URL parameter. Add URL and try again."
if [ $RETVAL -eq 6 ]; then
  echo "Unable to resolve host. Check URL and try again."
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"
  rm listening.htm
  exit 1

if grep -Fq "The site is listening..." listening.htm
  echo "Health is ok."
  echo "Service didn't respond.  Stopping."
  sleep 6 
  echo "Starting service."
  sleep 10
  echo "Restarted at `date`" >> monitor.log
  rm listening.htm
exit 1
share|improve this answer

Your Answer


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.