#!/bin/sh
# pg_autovacuum:   Starts and stops the PostgreSQL autovacuum daemon
# chkconfig: - 86 05
# description: Starts and stops the PostgreSQL autovacuum daemon
# processname: pg_autovacuum
# pidfile: /var/lib/pgsql/data/pg_autovacuum.pid

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

start(){
	echo -n $"Starting pg_autovacuum :"
	# Check if already running...

	pid=`pidof -s pg_autovacuum`
	if [ $pid ]
	then
		echo " [ PID: $pid ]"
	else
		su -l postgres -c "pg_autovacuum -D -U postgres > /dev/null 2>&1"&
		RETVAL=$?
		if [ $RETVAL -lt 1 ]
		then
			pid=`pidof -s pg_autovacuum`
			echo "$pid" > /var/lib/pgsql/data/pg_autovacuum.pid
			echo_success
		else
			echo_failure
		fi
		echo
	fi
}

stop(){
	echo -n $"Stopping pg_autovacuum :"
	pid=`pidof -s pg_autovacuum`
	if [ $pid ]
	then
		pid=${pid:0:6}
		kill -SIGTERM $(($pid))
		RETVAL=$?
		if [ $RETVAL -lt 1 ]; then
			echo_success
			rm -f /var/lib/pgsql/data/pg_autovacuum.pid
		else
			echo_failure
		fi
		echo
	else
		echo " [ NOT RUNNING ]"
		rm -f /var/lib/pgsql/data/pg_autovacuum.pid
	fi
}

restart(){
    stop
    start
}


# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
   reload)
	restart
	;;
   *)
	echo $"Usage: $0 {start|stop|restart|reload}"
	exit 1
esac

exit $RETVAL

