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

Here's the deal: I'm running a freebsd webserver with apache solr on a jetty

every now and then ( few times a month ) jetty quits and I have to restart it by typing:

service jetty start

I'm kinda new to creating unix scripts. Sure I should check why the service is stopping but for a quick fix I don't mind to let a cron run every half an hour to check if the service is still running.

I found this on a website but I don't fully understand what it's doing so I'm unable to modify it to my needs.

# check rc script supports status
    ${SERVICE} 2>&1 | /usr/bin/grep '|status|poll' >/dev/null
    if [ $? -eq 0 ]
    then
        # check status
        STATUS=$(${SERVICE} status)
        if [ $? -gt 0 ]
        then
            # service not running try to start
            echo ${STATUS}
            ${SERVICE} start
            ${SERVICE} status
        fi
    fi

So here is what I can make out of the above code:

{SERVICE} is a variable so I can replace it with 'service jetty' ?

something gets thrown away :)
if [something -equals 0]
 if something > 0
   starting service
 fi
fi

what does 2>&1 mean/do?

OR would it maybe be better to just run service jetty start from cron? Because if it is already running I get the response that is is already running.

share|improve this question
    
service jetty status ; echo $? Can you paste the output of this command to question. I need the output when jetty is running as well as in stopped state. I am asking since right now i don't have BSD server with me to test. –  Suku Jan 10 '13 at 12:42
    
jetty is running as pid 43839. 0 jetty is not running. 1 Also note to start jetty I need to do a sudo service jetty start –  FLY Jan 10 '13 at 12:49

6 Answers 6

up vote 1 down vote accepted

Have you thought of something like monit? it'll respond much more quickly than a cronjob, mail you notifications, and you can specify a health check to confirm the app is working correctly.

http://mmonit.com/monit/

share|improve this answer
    
hmm that's seams even a better solution! Thanks! –  FLY Jan 10 '13 at 13:23

Instead of creating a service in a crontab, you can add a small daemon to check if the service is running a reload it when it is not. Using the check condition of Suku, you can script a small daemon like this:

#!/bin/sh

reloader() {
  while :
  do
      sleep 5
      service httpd jetty || sudo service jetty start
  done
}

reloader &

This script will check every 5 seconds if your service is running, and won't consume much CPU. This is more precise than a job in a crontab.

Runing this script as root instead of putting some password as clear-text on the script itself may be more secure and no less efficient.

share|improve this answer
    
I Agree with the password ( tough I'm the only one accessing the server ) not sure about adding deamons to my system they seam evil >:) just kidding, I just don't enough know about deamons #n00b :) –  FLY Jan 10 '13 at 13:12
1  
If you are root on this server (and obviously you are) whatever solution you chose, you can run it as root; Avoiding this clear-text password (and will you remember editing your daemon/cronjob when you will have to change root password?) –  philippe Jan 10 '13 at 13:28

Try this way from your terminal. If it is working , put it in a crontab.

echo "PASSWORD" | sudo -S service jetty status && echo "Jetty is running" || sudo service jetty start

FYI :

  • || means logical OR and && means logical AND

  • 2>&1 means redirecting STDERR to STDOUT

  • 0 is the GOOD exit status in almost every program (Most programmers are programming in that way). So if service jetty status is exiting without error, it will just echo Jetty is
    running
    . Other wise it will start the service.

  • sudo -S - From man page :- "The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character."

share|improve this answer
    
I'm getting httpd does not exist in /etc/rc.d or the local startup –  FLY Jan 10 '13 at 13:01
    
oh.. sorry typo.. edited the answer.. –  Suku Jan 10 '13 at 13:02
    
I see, somewhat different way of 'programming' than I used to. basically your echo the pass and try get the status if status = 1 do the && echo "jetty is running" else start the service ? –  FLY Jan 10 '13 at 13:06
1  
Added explanation to answer –  Suku Jan 10 '13 at 13:13

I would use a more comprehensive script which stores the process ID and is much safer.

#!/bin/bash

processId=$(service jetty start)          # Start yetty
processId=$!      # stores the process ID of jetty

while sleep 30
do
    if kill -0 $processId # Check if process is still running 
    then
        echo >&2 "Process is running." 
    else
        echo >&2 "ERROR - terminated"
   break
    fi
done

Put the script in a cron and chech occassionally. See for more examples and good tutorials @ http://mywiki.wooledge.org/BashFAQ#BashFAQ.2BAC8-042.How_can_I_find_out_if_a_process_is_still_running.3F

share|improve this answer

As you said in your question, you need to investigate the reason stopping your service.

Anyway, you can try monit. It is a daemon that can be configured to monit your services and restart if required. So, no need to write any script for this. Also, logging helps you know when the service is stopped and restarted.

share|improve this answer

status command should be supported by all rc.d scripts. You can use this script to restart dead daemons:

. /etc/rc.subr

for service in ${autorestart_services}; do
    /usr/local/etc/rc.d/${service} status >/dev/null
    if [ $? != 0 ]; then
        echo "System service ${service} is down. Try to restart..."
        /usr/local/etc/rc.d/${service} restart
    fi
done
share|improve this answer

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.