vi /sbin/init.d/sfmdb

#! /bin/sh
# sfmdb This is the init script for starting up the
#               HP System Management verison of PostgreSQL
#
# processname: postmaster
# pidfile: /var/opt/sfmdb/pgsql/postmaster.pid

# Version 6.5.3-2 Lamar Owen
# Added code to determine if PGDATA exists, whether it is current version
#     or not, and initdb if no PGDATA (initdb will not overwrite a database).

# Version 7.0 Lamar Owen
# Added logging code
# Changed PGDATA.
#

# Version 7.0.2 Trond Eivind Glomsrod <teg@redhat.com>
# use functions, add conditional restart

# Version 7.0.3 Lamar Owen <lamar@postgresql.org>
# Check for the existence of functions before blindly using them
# in particular -- check for success () and failure () before using.
# More Cross-distribution support -- PGVERSION variable, and docdir checks.
# Version 7.1 Release Candidate Lamar Owen <lamar@postgresql.org>
# initdb parameters have changed.

# Version 7.1.2 Trond Eivind Glomsrod <teg@redhat.com>
# Specify shell for su
# Handle stop better - kill unwanted output, make it wait until the database is
# Handle locales slightly differently - always using "C" isn't a valid option
# Kill output from database initialization
# Mark messages for translation

# Version 7.1.2-2.PGDG Lamar Owen <lamar.owen@wgcr.org>
# sync up.
# Karl's fixes for some quoting issues.

# Version 7.2b2 Lamar Owen <lamar.owen@wgcr.org>
# version change.

# Version 7.2 final.  Lamar Owen <lamar.owen@wgcr.org>
# reload from Peter E.
# Tested the $? return for the stop script -- it does in fact propagate.
# TODO: multiple postmasters.

PGSQL_DATA=/var/opt/sfmdb/pgsql
PGSQL_DIR=/opt/sfmdb/pgsql
PGSQL_PID=$PGSQL_DATA/postmaster.pid

export PGSQL_DATA
export PGSQL_DIR
export PGSQL_PID

PATH=$PGSQL_DIR/bin:/sbin:/usr/sbin:/usr/bin
SHLIB_PATH=$PGSQL_DIR/lib:$SHLIB_PATH
export PATH
export SHLIB_PATH

start() {
        # Check for PGDATA structure
        if [ ! -d $PGSQL_DATA/base ]
        then
            echo "Initializing database. . ."
            if [ ! -d $PGSQL_DATA ]
            then
                mkdir -p $PGSQL_DATA
            fi
           chown sfmdb $PGSQL_DATA
            chmod 700 $PGSQL_DATA
            su sfmdb -c '$PGSQL_DIR/bin/initdb \
                    -D $PGSQL_DATA' < /dev/null
            sleep 1
        fi
        # Check if postmaster is already running...
        ps -ef | grep -v grep | grep $PGSQL_DIR/bin/postmaster  > /dev/null
        if [ $? -eq 0 ] && [ -e $PGSQL_PID ]
        then
            echo "sfmdb postgresql is already running"
        else
            rm -f /tmp/.s.PGSQL.10864* > /dev/null
            echo "Starting sfmdb postgresql service!"
            su  sfmdb -c '$PGSQL_DIR/bin/pg_ctl  -o -i -D $PGSQL_DATA -p $PGSQL_
DIR/bin/postmaster start >> $PGSQL_DATA/sfmdb.log 2>&1' < /dev/null
            sleep 1
        fi
}

my_stop() {
        echo  "Stopping sfmdb postgresql service: "
        su  sfmdb -c '$PGSQL_DIR/bin/pg_ctl stop -D $PGSQL_DATA -s -m fast'

  ret=$?
        if [ $ret -eq 0 ]
        then
           if  [ -e $PGSQL_PID ]
           then
               pgsql_pid=$(cat $PGSQL_PID)
               kill $pgsql_pid > /dev/null 2>&1
               sleep 2
               rm -f $PGSQL_PID > /dev/null 2>&1
            fi
        fi
        rm -f /tmp/.s.PGSQL.10864* > /dev/null
}

# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)

# See how we were called.
case "$1" in
  start_msg)
        echo "Starting sfmdb PostgreSQL daemons"
        exit 0
       ;;
  stop_msg)
        echo "Stopping sfmdb PostgreSQL daemons"
        exit 0
        ;;
  start)
        start
        ;;
  stop)
        my_stop
        ;;
  restart)
        my_stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0