#!/bin/bash url='http://www.sitetomonitor.com/provider_search?utf8=%E2%9C%93&search%5Bquery%5D=aba+therapist&search%5Blocation%5D=San+Francisco%2C+CA&commit=Search' self=$(basename $0) if [ -w "/var/run" ]; then pidfile="/var/run/$self.pid" else pidfile="/tmp/$self.pid" fi if [ -w "/var/log" ]; then logfile="/var/log/$self.log" else logfile="/tmp/$self.log" fi install_dir=/app/current solr_pidfile=$install_dir/tmp/pids/production/sunspot-solr.pid function log() { echo "$(hostname) $(date): $1" >> $logfile } function tee() { echo "$1" log "$1" } function heartbeatd() { while : do status=$(curl -sw "%{http_code}" -o /dev/null "$url") if [ $status == "200" ]; then log "200 OK" sleep 15 else log "Problem - Received ($status) code" if [ -f $solr_pidfile ]; then log 'restarting solr on host' (cd $install_dir && kill -9 $(cat $solr_pidfile) && rake search:start RAILS_ENV=production) sleep 60 fi fi done } case $1 in start) if [ -f $pidfile ]; then ps $(cat $pidfile) 2>&1 > /dev/null if [ $? -eq 0 ]; then echo 'already running...exiting!' exit else rm $pidfile fi fi heartbeatd 2>&1 >> $logfile & echo $! > $pidfile tee "server started with pid $!" ;; stop) tee "Shutting down the server now" if [ -f $pidfile ]; then kill $(cat $pidfile) 2>&1 > /dev/null if [ $? -eq 0 ]; then rm $pidfile tee 'complete' exit 0 else tee 'ERROR!' tee 'Uable to stop server: kill manually' tee "pidfile: $pidfile" exit 1 fi else tee 'no pid file...exiting' exit 1 fi ;; status) if [ -f $pidfile ]; then ps $(cat $pidfile) 2>&1 > /dev/null if [ $? -eq 0 ]; then echo 'running' fi else echo 'not running' fi ;; *) echo 'usage: solr_heartbeatd [start|stop|status]' exit 1 ;; esac