slackbuilds

My Slackbuilds
git clone git://git.server.ky/slackcoder/slackbuilds
Log | Files | Refs | README

rc.postgresql.new (4999B)


      1 #!/bin/bash
      2 
      3 # PostgreSQL startup script for Slackware Linux
      4 #
      5 # $Revision$
      6 # $Date$
      7 #
      8 # Copyright 2007-2018 Adis Nezirovic <adis_at_linux.org.ba>
      9 # All rights reserved.
     10 #
     11 # Redistribution and use of this script, with or without modification, is
     12 # permitted provided that the following conditions are met:
     13 #
     14 # 1. Redistributions of this script must retain the above copyright
     15 #    notice, this list of conditions and the following disclaimer.
     16 #
     17 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
     18 #  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 #  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
     20 #  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 #  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22 #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23 #  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24 #  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25 #  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26 #  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 # Do not source this script (since it contains exit() calls)
     29 #
     30 # Since version 9.3 this startup script can run multiple PostgreSQL
     31 # versions on different ports and with different data dirs.
     32 # 
     33 # e.g. PG_VERSION=14 PG_PORT=6432 /etc/rc.d/rc.@PRGNAM@ start
     34 
     35 PG_VERSION=${PG_VERSION:-@PG_VERSION@}
     36 PG_PORT=${PG_PORT:-@PG_PORT@}
     37 LIBDIRSUFFIX="@LIBDIRSUFFIX@"
     38 LOGFILE=/var/log/@PRGNAM@-$PG_VERSION
     39 DATADIR=/var/lib/pgsql/$PG_VERSION/data
     40 POSTGRES=/usr/lib${LIBDIRSUFFIX}/@PRGNAM@/$PG_VERSION/bin/postgres
     41 PG_CTL=/usr/lib${LIBDIRSUFFIX}/@PRGNAM@/$PG_VERSION/bin/pg_ctl
     42 PIDFILE=$DATADIR/postmaster.pid
     43 
     44 # oom-killer score
     45 #
     46 # https://www.postgresql.org/docs/14/kernel-resources.html#LINUX-MEMORY-OVERCOMMIT
     47 PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
     48 PG_MASTER_OOM_SCORE_ADJ=-1000
     49 PG_CHILD_OOM_SCORE_ADJ=0
     50 PG_ENV="PG_OOM_ADJUST_FILE=$PG_OOM_ADJUST_FILE PG_OOM_ADJUST_VALUE=$PG_CHILD_OOM_SCORE_ADJ"
     51 
     52 # Return values (according to LSB):
     53 # 0 - success
     54 # 1 - generic or unspecified error
     55 # 2 - invalid or excess argument(s)
     56 # 3 - unimplemented feature (e.g. "reload")
     57 # 4 - insufficient privilege
     58 # 5 - program is not installed
     59 # 6 - program is not configured
     60 # 7 - program is not running
     61 
     62 pg_ctl()
     63 {
     64 	CMD="$PG_CTL -o '-p $PG_PORT' $@"
     65 	su - postgres -c "$PG_ENV $CMD"
     66 }
     67 
     68 if [ ! -f $POSTGRES ]; then
     69 	echo "Could not find 'postgres' binary. Maybe PostgreSQL is not installed properly?"
     70 	exit 5
     71 fi
     72 
     73 case "$1" in
     74 
     75 	"start")
     76 		echo "Starting PostgreSQL"
     77 		touch $LOGFILE
     78 		chown postgres:wheel $LOGFILE
     79 		chmod 0640 $LOGFILE
     80 	
     81 		if [ ! -e $DATADIR/PG_VERSION ]; then
     82 			echo "You should initialize the PostgreSQL database at location $DATADIR"
     83 			echo "e.g. su postgres -c \"initdb -D $DATADIR --locale=en_US.UTF-8 -A md5 -W\""
     84 			exit 6
     85 		fi
     86 	
     87 		if [ $(pgrep -f $POSTGRES) ]; then
     88 
     89 			echo "PostgreSQL daemon already running"
     90 			if [ ! -f $PIDFILE ]; then
     91 				echo "Warning: Missing pid file $PIDFILE"
     92 			fi
     93 			exit 1
     94 
     95 		else
     96 			test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
     97 			pg_ctl start -w -l $LOGFILE -D $DATADIR
     98 			exit 0
     99 		fi
    100 	;;
    101 
    102 	"stop")
    103 		echo "Shutting down PostgreSQL..."
    104 		pg_ctl stop -l $LOGFILE -D $DATADIR -m smart
    105 	;;
    106 
    107 	"force-stop")
    108 		# Take care! This will kill _all_ client connections
    109 		# and rollback current transactions.
    110 		echo "Shutting down PostgreSQL (fast)..."
    111 		pg_ctl stop -l $LOGFILE -D $DATADIR -m fast
    112 	;;
    113 
    114 	"unclean-stop")
    115 		# Take care! This will abort server process itself
    116 		# resulting with database recovery on next start.
    117 		echo "Shutting down PostgreSQL (immediate)..."
    118 		pg_ctl stop -l $LOGFILE -D $DATADIR -m immediate
    119 	;;
    120 
    121 	"restart")
    122 		echo "Restarting PostgreSQL..."
    123 		test -e "$PG_OOM_ADJUST_FILE" && echo "$PG_MASTER_OOM_SCORE_ADJ" > "$PG_OOM_ADJUST_FILE"
    124 		pg_ctl restart -l $LOGFILE -D $DATADIR -m smart
    125 	;;
    126 
    127 	"force-restart")
    128 		# Take care! This will kill _all_ client connections
    129 		# and rollback current transactions.
    130 		echo "Restarting PostgreSQL (fast)..."
    131 		pg_ctl restart -l $LOGFILE -D $DATADIR -m fast
    132 	;;
    133 
    134 	"unclean-restart")
    135 		# Take care: This will abort server process itself
    136 		# resulting with database recovery on start.
    137 		echo "Restarting PostgreSQL (immediate)..."
    138 		pg_ctl restart -l $LOGFILE -D $DATADIR -m immediate
    139 	;;
    140 
    141 	"reload")
    142 		echo "Reloading configuration for PostgreSQL..."
    143 		pg_ctl reload -l $LOGFILE -D $DATADIR -m smart
    144 	;;
    145 
    146 	"status")
    147 		if [ $(pgrep -f $POSTGRES) ]; then
    148 			echo "PostgreSQL is running"
    149 
    150 			if [ ! -e $PIDFILE ]; then
    151 				echo "Warning: Missing pid file $PIDFILE"
    152 			fi
    153 
    154 			exit 0
    155 		else
    156 			echo "PostgreSQL is stopped"
    157 
    158 			if [ -e $PIDFILE ]; then
    159 				echo "Detected stale pid file $PIDFILE"
    160 			fi
    161 
    162 			exit 0
    163 		fi
    164 	;;
    165 
    166 	*)
    167 		# unclean-stop and unclean-restart are not documented on purpose.
    168 		echo "Usage: $0 {start|stop|force-stop|status|restart|force-restart|reload}"
    169 		exit 1
    170 	;;
    171 esac