#!/bin/bash
#
# gearmand        Startup script for the Gearman server
#
# chkconfig: - 85 15
# description: Gearman is a distributed job system.
# processname: gearmand
# config: /etc/sysconfig/gearmand
# pidfile: /var/run/gearmand/gearmand.pid
#
### BEGIN INIT INFO
# Provides: gearmand
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start:
# Default-Stop:
# Short-Description: start and stop the Gearman server
# Description: Gearman is a distributed job system.
### END INIT INFO

# Source function library.
. /etc/rc.status

if [ -f /etc/sysconfig/gearmand ]; then
        . /etc/sysconfig/gearmand
fi

[ -z "${PIDFILE}" ] && pidfile="/var/run/gearmand/gearmand.pid"

gearmand=/usr/sbin/gearmand
prog=gearmand

RETVAL=0

start() {
        echo -n $"Starting $prog: "
        startproc -p $pidfile -u gearmand $gearmand -d $OPTIONS
        RETVAL=$?
        rc_status -v
        [ $RETVAL = 0 ] && pgrep -f $gearmand > $pidfile
        return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	pid=$(cat $pidfile)
	killproc -p $pidfile $gearmand -TERM
	RETVAL=$?
	ps -p $pid 2>/dev/null && sleep 2 && kill $pid 2>/dev/null && sleep 2 && kill -2 $pid 2>/dev/null
	rc_status -v
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	echo -n "Checking for gearmand: "
	checkproc -p $pidfile $gearmand
	rc_status -v
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	rc_status
	;;
  condrestart|try-restart)
	if checkproc -p $pidfile $gearmand >&/dev/null; then
		stop
		start
		rc_status
	fi
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|reload|condrestart|status|help}"
	RETVAL=3
esac

exit $RETVAL

