#!/bin/sh # # dm-postgresql # # Starts the PostgreSQL server used for DM software # # Modified from the original RHEL postgresql init.d script # # chkconfig: 345 97 97 # description: controls Dm database server ### BEGIN INIT INFO # Provides: dm-postgresql # Required-Start: $local_fs # Required-Stop: $local_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Description: controls Dm database server ### END INIT INFO # PGVERSION is the full package version, e.g., 8.4.0 #PGVERSION=9.3.4 PGVERSION=9.6.1 # PGMAJORVERSION is major version, e.g., 8.4 (this should match PG_VERSION) PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'` # Source function library. . /etc/rc.d/init.d/functions # Get function listing for cross-distribution logic. TYPESET=`typeset -f|grep "declare"` # Get config. . /etc/sysconfig/network # Find the name of the script # NAME=`basename $0` NAME="DM Database" # For SELinux we need to use 'runuser' not 'su' SU=su if [ -x /sbin/runuser ]; then SU=runuser fi # Check if we are root before running command runCommand() { _cmd="$@" if [ `id -u` = 0 ]; then $SU -l $PGUSER -c "_$cmd" >> $PGSTARTUPLOG 2>&1 < /dev/null else eval "$_cmd" >> $PGSTARTUPLOG 2>&1 < /dev/null fi } # Set defaults for configuration variables if [ -z $DM_ROOT_DIR ]; then myDir=`dirname $0` currentDir=`pwd` && cd $myDir/../.. export DM_ROOT_DIR=`pwd` cd $currentDir fi DM_SETUP_FILE=$DM_ROOT_DIR/setup.sh if [ ! -f $DM_SETUP_FILE ]; then echo "Setup file $DM_SETUP_FILE does not exist." exit 2 fi . $DM_SETUP_FILE > /dev/null PGROOT=$DM_OPT_DIR/postgresql/$DM_HOST_ARCH PGENGINE=$PGROOT/bin #PGUSER=dm PGUSER=`whoami` PGGROUP=`groups $PGUSER | cut -f3 -d ' '` PGPORT=11136 # 111-DM PGDATA=$PGROOT/data PGRUNDIR=$DM_INSTALL_DIR/var/run PGLOGDIR=$DM_INSTALL_DIR/var/log PGSTARTUPLOG=$PGLOGDIR/postgresql PGPIDFILE=$PGRUNDIR/postmaster.pid PGLOCKFILE=$PGRUNDIR/postmaster.lock mkdir -p $PGDATA && chown -R $PGUSER:$PGGROUP $PGDATA || exit 1 mkdir -p $PGRUNDIR && chown -R $PGUSER:$PGGROUP $PGRUNDIR || exit 1 mkdir -p $PGLOGDIR && chown -R $PGUSER:$PGGROUP $PGLOGDIR || exit 1 export PGDATA export PGPORT export LD_LIBRARY_PATH=$PGROOT/lib # Check that networking is up. # Pretty much need it for postmaster. [ "${NETWORKING}" = "no" ] && exit 1 [ -f "$PGENGINE/postmaster" ] || exit 1 script_result=0 start() { PSQL_START=$"Starting ${NAME} service: " # Make sure startup-time log file is valid if [ ! -e "$PGSTARTUPLOG" -a ! -h "$PGSTARTUPLOG" ]; then touch "$PGSTARTUPLOG" || exit 1 chown $PGUSER:$PGGROUP "$PGSTARTUPLOG" chmod go-rwx "$PGSTARTUPLOG" [ -x /sbin/restorecon ] && /sbin/restorecon "$PGSTARTUPLOG" fi # Check for the PGDATA structure if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]; then # Check version of existing PGDATA if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ]; then SYSDOCDIR="(Your System's documentation directory)" echo echo $"An old version of the database format was found." echo $"You need to upgrade the data format before using PostgreSQL." echo $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information." exit 1 fi # No existing PGDATA! Warn the user to initdb it. else echo echo "$PGDATA is missing. Use \"$0 initdb\" to initialize the cluster first." echo_failure echo exit 1 fi echo -n "$PSQL_START" cmd="$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" runCommand $cmd sleep 2 pid=`pidof -s "$PGENGINE/postmaster"` if [ $pid ] && [ -f "$PGDATA/postmaster.pid" ]; then success "$PSQL_START" touch $PGLOCKFILE head -n 1 "$PGDATA/postmaster.pid" > $PGPIDFILE echo else failure "$PSQL_START" echo script_result=1 fi } stop() { echo -n $"Stopping ${NAME} service: " cmd="export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; $PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" runCommand $cmd ret=$? if [ $ret -eq 0 ]; then echo_success else echo_failure script_result=1 fi echo rm -f $PGPIDFILE rm -f $PGLOCKFILE } restart() { stop start } condrestart() { [ -e $PGLOCKFILE ] && restart } condstop() { [ -e $PGLOCKFILE ] && stop } reload() { cmd="export LD_LIBRARY_PATH=$LD_LIBRARY_PATH; $PGENGINE/pg_ctl reload -D '$PGDATA' -s" runCommand $cmd } initdb() { if [ -f "$PGDATA/PG_VERSION" ]; then echo -n "Data directory is not empty!" echo_failure echo script_result=1 else echo -n $"Initializing database: " if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]; then mkdir -p "$PGDATA" || exit 1 chown $PGUSER:$PGGROUP "$PGDATA" chmod go-rwx "$PGDATA" fi # Clean up SELinux tagging for PGDATA [ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA" # Make sure the startup-time log file is OK, too if [ ! -e "$PGSTARTUPLOG" -a ! -h "$PGSTARTUPLOG" ]; then touch "$PGSTARTUPLOG" || exit 1 chown $PGUSER:$PGGROUP "$PGSTARTUPLOG" chmod go-rwx "$PGSTARTUPLOG" [ -x /sbin/restorecon ] && /sbin/restorecon "$PGSTARTUPLOG" fi # Initialize the database cmd="$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" runCommand $cmd # Create directory for postmaster log mkdir -p "$PGDATA/pg_log" chown $PGUSER:$PGGROUP "$PGDATA/pg_log" chmod go-rwx "$PGDATA/pg_log" if [ -f "$PGDATA/PG_VERSION" ]; then echo_success else echo_failure script_result=1 fi echo fi } # 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) start ;; stop) stop ;; status) status postmaster script_result=$? ;; restart) restart ;; condrestart) condrestart ;; condstop) condstop ;; reload|force-reload) reload ;; initdb) initdb ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop|reload|force-reload|initdb}" exit 1 esac exit $script_result