Freebsd creating a script to check if service is active and start the service if not

freebsdscriptingservice

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.

Best Answer

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/