Init scripts for pg_autovacuum

From: Marc Gimpel <mgimpel(at)horizonwimba(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: Init scripts for pg_autovacuum
Date: 2005-05-17 09:10:43
Message-ID: 4289B513.90400@horizonwimba.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hi

I've improved the pg_autovacuum SysV init script so that it can
work stand alone (place it in /etc/rc.d/init.d/autovacuum and use
chkconfig to turn it on or off) or it can also work with the postgresql
init script.
Someone may like to consider putting it into CVS.
Here is the autovacuum init script:

**********************************************************************

#!/bin/sh
# autovacuum This is the init script for starting up the PostgreSQL
# server AutoVacuum daemon.
#
# chkconfig: - 95 05
# description: Starts and stops the PostgreSQL AutoVacuum daemon.
# processname: pg_autovacuum
# pidfile: /var/run/pg_autovacuum.${PGPORT}.pid
#
# Version 0.9 Marc Gimpel <marc(at)gimpel(dot)org>
# Based on script by Theo Galanakis 11/04/2005
# Based on postgresql init script

# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions

# Name of database application
PGNAME=postgresql

# Set defaults for configuration variables
PGENGINE=/usr/bin
PGPORT=5432
PGAV_SLEEP=600

# Override defaults from /etc/sysconfig/pgsql if file is present
[ -f /etc/sysconfig/pgsql/${PGNAME} ] && . /etc/sysconfig/pgsql/${PGNAME}

# Initialize pg_autovacuum defaults
if [ -z "$PGAV_LOGFILE" ]; then
PGAV_LOGFILE=/var/log/pg_autovacuum.{$PGPORT}.log
fi

script_result=0

startautovacuum() {
# Start pg_autovacuum. Theo Galanakis 11/05/2005
# Note : ensure $PGENGINE is being used in script otherwise
hardcode to /usr/bin/
if [ -f /var/run/pg_autovacuum.${PGPORT}.pid ]
then
echo $"pg_autovacuum already running."
else
PGAV_START=$"Starting pg_autovacuum service: "
echo -n "$PGAV_START"
pg_autovacuum -D -s ${PGAV_SLEEP} -p ${PGPORT} -U
postgres -L ${PGAV_LOGFILE} ${PGAV_OPTS}
sleep 1
pg_autovacuum_pid=`pidof -s $PGENGINE/pg_autovacuum`
if [ $pg_autovacuum_pid ]
then
success "$PGAV_START"
echo $pg_autovacuum_pid >
/var/run/pg_autovacuum.${PGPORT}.pid
echo
else
failure "$PGAV_START"
echo
script_result=1
fi
fi
}

stopautovacuum () {
# Stop pg_autovacuum.
if [ -f /var/run/pg_autovacuum.${PGPORT}.pid ]
then
pg_autovacuum_pid=`head -n 1
/var/run/pg_autovacuum.${PGPORT}.pid`
ret=`ps --no-heading ${pg_autovacuum_pid}`
if [ -z "$ret" ]
then
echo $"pg_autovacuum: pid
[${pg_autovacuum_pid}] stored in /var/run/pg_autovacuum.${PGPORT}.pid
does not exist."

else
echo -n $"Stopping pg_autovacuum service: "

kill -TERM $pg_autovacuum_pid
ret=`ps --no-heading ${pg_autovacuum_pid}`
if [ -z "$ret" ]
then
echo_success
else
echo_failure
script_result=1
fi
echo
rm -f /var/run/pg_autovacuum.${PGPORT}.pid
fi
else
echo $"pg_autovacuum is not running."
fi
}

statusautovacuum() {
# Status pg_autovacuum.
if [ -f /var/run/pg_autovacuum.${PGPORT}.pid ]
then
pg_autovacuum_pid=`head -n 1
/var/run/pg_autovacuum.${PGPORT}.pid`
ret=`ps --no-heading ${pg_autovacuum_pid}`
if [ -z "$ret" ]
then
echo $"pg_autovacuum not running..."
else
echo $"pg_autovacuum (pid ${pg_autovacuum_pid})
is running..."
fi
else
status pg_autovacuum
fi
}

restartautovacuum() {
stopautovacuum
startautovacuum
}

case "$1" in
start)
startautovacuum
;;
stop)
stopautovacuum
;;
status)
statusautovacuum
;;
restart)
restartautovacuum
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac

exit $script_result

**********************************************************************

To use the script with the postgresql init script just slightly modify
it as follows:

**********************************************************************

case "$1" in
start)
start
if [ -f "$INITD/autovacuum" ] && [ "$PGAV_ENABLED" = "yes" ]; then
$INITD/autovacuum start
fi
;;
stop)
stop
if [ -f "$INITD/autovacuum" ] && [ "$PGAV_ENABLED" = "yes" ]; then
$INITD/autovacuum stop
fi
;;
status)
status postmaster
script_result=$?
if [ -f "$INITD/autovacuum" ] && [ "$PGAV_ENABLED" = "yes" ]; then
$INITD/autovacuum status
fi
;;
restart)
restart
if [ -f "$INITD/autovacuum" ] && [ "$PGAV_ENABLED" = "yes" ]; then
$INITD/autovacuum restart
fi
;;
condrestart)
condrestart
;;
condstop)
condstop
;;
reload|force-reload)
reload
;;
*)
echo $"Usage: $0
{start|stop|status|restart|condrestart|condstop|reload|force-reload}"
exit 1
esac

**********************************************************************

Note that if the autovacuum script is not found there will be no error
so it is safe to commit these changes in CVS too.

Finally you just need to add the following line to
/etc/sysconfig/pgsql/postgresql to enable autovacuuming.

PGAV_ENABLED=yes

So none of the changes will affect peoples current setups unless they
explicitely chose to.

I hope this helps people.

Marc

--
--------------------------------------------------------------------------
Marc Gimpel
Director of Research

HORIZON WIMBA - REACH BEYOND THE CLASSROOM
Espace Beethoven, bat 2B, 1200 Route des Lucioles
06560 Sophia Antipolis, France
Tel. + 33 (0)4 92 00 45 85
Fax + 33 (0)4 92 00 45 81
Mob + 33 (0)6 74 88 02 72
mgimpel(at)horizonwimba(dot)com

www.horizonwimba.com
--------------------------------------------------------------------------

Browse pgsql-admin by date

  From Date Subject
Next Message Bruno Wolff III 2005-05-17 13:34:54 Re: user specific limit for connections
Previous Message KÖPFERL Robert 2005-05-17 09:03:22 user specific limit for connections