#!/bin/sh # # postgresql This is the init script for starting up the PostgreSQL # server # # chkconfig: 345 84 25 # # description: Starts and stops the PostgreSQL backend daemon that handles \ # all database requests. # # processname: postmaster # Source function library . /etc/rc.d/init.d/functions # Get network config . /etc/sysconfig/network # Get service config if [ -f /etc/sysconfig/postgresql ] ; then . /etc/sysconfig/postgresql else echo "Error: /etc/sysconfig/postgresql not found" echo " PostgreSQL can't be run." exit 1 fi # Check that networking is up. if is_no "${NETWORKING}"; then msg_network_down PostgreSQL exit 1 fi pgstatus () { PG_STATUS="unknown" PG_PID="unknown" status=`/usr/bin/pg_ctl -D $1 status` if echo "$status" | grep -q 'pg_ctl: postmaster or postgres is not running'; then PG_STATUS="not running" elif echo "$status" | grep -q 'pg_ctl: postmaster is running'; then PG_STATUS="running" PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1` fi } action="$1" # any db cluster as command line argument? if [ $# -gt 1 ]; then shift POSTGRES_DB_CLUSTERS="$@" fi # See how we were called. case "$action" in start) for pgdir in $POSTGRES_DB_CLUSTERS; do pgstatus "$pgdir" if [ "$PG_STATUS" = "running" ]; then if ps ax | grep -v grep | grep -q "$PG_PID"; then msg_already_running "PostgreSQL $pgdir" else msg_not_running "PostgreSQL $pgdir" rm -f $pgdir/postmaster.pid $0 start "$pgdir" fi else msg_starting "PostgreSQL $pgdir" busy TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir start 2>&1 >/dev/null" pgstatus "$pgdir" if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then deltext; ok else deltext; fail fi fi done ;; stop) for pgdir in $POSTGRES_DB_CLUSTERS; do pgstatus "$pgdir" if [ "$PG_STATUS" = "not running" ]; then msg_not_running "PostgreSQL $pgdir" else msg_stopping "PostgreSQL $pgdir" busy if ps ax | grep -v grep | grep -q "$PG_PID"; then TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null" pgstatus "$pgdir" if [ "$PG_STATUS" != "not running" ]; then deltext; fail else deltext; ok fi else deltext; died fi rm -f $pgdir/postmaster.pid fi done ;; status) for pgdir in $POSTGRES_DB_CLUSTERS; do TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status" done ;; restart) $0 stop "$POSTGRES_DB_CLUSTERS" $0 start "$POSTGRES_DB_CLUSTERS" ;; reload) for pgdir in $POSTGRES_DB_CLUSTERS; do pgstatus "$pgdir" if [ "$PG_STATUS" = "not running" ]; then msg_not_running "PostgreSQL $pgdir" else msg_reloading "PostgreSQL $pgdir" busy if ps ax | grep -v grep | grep -q "$PG_PID"; then TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null" pgstatus "$pgdir" if [ "$PG_STATUS" = "running" ]; then deltext; ok else deltext; fail fi else deltext; died rm -f $pgdir/postmaster.pid $0 start fi fi done ;; *) msg_usage "$0 {start|stop|status|restart|reload}" exit 1 esac exit 0