Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # postgresql This is the init script for starting up the PostgreSQL |
| 4 | # server. |
| 5 | # |
| 6 | # chkconfig: - 64 36 |
| 7 | # description: PostgreSQL database server. |
| 8 | # processname: postmaster |
| 9 | # pidfile: /var/run/postmaster.PORT.pid |
| 10 | |
| 11 | # This script is slightly unusual in that the name of the daemon (postmaster) |
| 12 | # is not the same as the name of the subsystem (postgresql) |
| 13 | |
| 14 | # PGVERSION is the full package version, e.g., 8.4.0 |
| 15 | # Note: the specfile inserts the correct value during package build |
| 16 | PGVERSION=9.2.4 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 17 | # PGMAJORVERSION is major version, e.g., 10 (this should match PG_VERSION) |
| 18 | PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\).*$/\1/'` |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 19 | |
| 20 | # Source function library. |
| 21 | . /etc/init.d/functions |
| 22 | |
| 23 | # Find the name of the script |
| 24 | NAME=`basename $0` |
| 25 | if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ] |
| 26 | then |
| 27 | NAME=${NAME:3} |
| 28 | fi |
| 29 | |
| 30 | # For SELinux we need to use 'runuser' not 'su' |
| 31 | if [ -x /sbin/runuser ] |
| 32 | then |
| 33 | SU=runuser |
| 34 | else |
| 35 | SU=su |
| 36 | fi |
| 37 | |
| 38 | |
| 39 | # Set defaults for configuration variables |
| 40 | PGENGINE=/usr/bin |
| 41 | PGPORT=5432 |
| 42 | PGDATA=/var/lib/postgresql/data |
| 43 | PGLOG=/var/lib/postgresql/pgstartup.log |
| 44 | # Value to set as postmaster process's oom_adj |
| 45 | PG_OOM_ADJ=-17 |
| 46 | |
| 47 | # Override defaults from /etc/sysconfig/postgresql if file is present |
| 48 | [ -f /etc/default/postgresql/${NAME} ] && . /etc/default/postgresql/${NAME} |
| 49 | |
| 50 | export PGDATA |
| 51 | export PGPORT |
| 52 | |
| 53 | lockfile="/var/lock/subsys/${NAME}" |
| 54 | pidfile="/var/run/postmaster.${PGPORT}.pid" |
| 55 | |
| 56 | script_result=0 |
| 57 | |
| 58 | start(){ |
| 59 | [ -x "$PGENGINE/postmaster" ] || exit 5 |
| 60 | |
| 61 | PSQL_START=$"Starting ${NAME} service: " |
| 62 | |
| 63 | # Make sure startup-time log file is valid |
| 64 | if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ] |
| 65 | then |
| 66 | touch "$PGLOG" || exit 4 |
| 67 | chown postgres:postgres "$PGLOG" |
| 68 | chmod go-rwx "$PGLOG" |
| 69 | [ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG" |
| 70 | fi |
| 71 | |
| 72 | # Check for the PGDATA structure |
| 73 | if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ] |
| 74 | then |
| 75 | # Check version of existing PGDATA |
| 76 | if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ] |
| 77 | then |
| 78 | SYSDOCDIR="(Your System's documentation directory)" |
| 79 | if [ -d "/usr/doc/postgresql-$PGVERSION" ] |
| 80 | then |
| 81 | SYSDOCDIR=/usr/doc |
| 82 | fi |
| 83 | if [ -d "/usr/share/doc/postgresql-$PGVERSION" ] |
| 84 | then |
| 85 | SYSDOCDIR=/usr/share/doc |
| 86 | fi |
| 87 | if [ -d "/usr/doc/packages/postgresql-$PGVERSION" ] |
| 88 | then |
| 89 | SYSDOCDIR=/usr/doc/packages |
| 90 | fi |
| 91 | if [ -d "/usr/share/doc/packages/postgresql-$PGVERSION" ] |
| 92 | then |
| 93 | SYSDOCDIR=/usr/share/doc/packages |
| 94 | fi |
| 95 | echo |
| 96 | echo $"An old version of the database format was found." |
| 97 | echo $"You need to upgrade the data format before using PostgreSQL." |
| 98 | echo $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information." |
| 99 | exit 1 |
| 100 | fi |
| 101 | else |
| 102 | # No existing PGDATA! Warn the user to initdb it. |
| 103 | echo |
| 104 | echo "$PGDATA is missing. Use \"postgresql-setup initdb\" to initialize the cluster first." |
| 105 | echo -n " [FAILED] " |
| 106 | echo |
| 107 | exit 1 |
| 108 | fi |
| 109 | |
| 110 | echo -n "$PSQL_START" |
| 111 | test x"$PG_OOM_ADJ" != x && echo "$PG_OOM_ADJ" > /proc/self/oom_score_adj |
| 112 | $SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null |
| 113 | sleep 2 |
| 114 | pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null` |
| 115 | if [ "x$pid" != x ] |
| 116 | then |
| 117 | echo -n " [ OK ]" |
| 118 | touch "$lockfile" |
| 119 | echo $pid > "$pidfile" |
| 120 | echo |
| 121 | else |
| 122 | echo -n " [FAILED]" |
| 123 | echo |
| 124 | script_result=1 |
| 125 | fi |
| 126 | } |
| 127 | |
| 128 | stop(){ |
| 129 | echo -n $"Stopping ${NAME} service: " |
| 130 | if [ -e "$lockfile" ] |
| 131 | then |
| 132 | $SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null |
| 133 | ret=$? |
| 134 | if [ $ret -eq 0 ] |
| 135 | then |
| 136 | echo -n " [ OK ] " |
| 137 | rm -f "$pidfile" |
| 138 | rm -f "$lockfile" |
| 139 | else |
| 140 | echo -n " [FAILED] " |
| 141 | script_result=1 |
| 142 | fi |
| 143 | else |
| 144 | # not running; per LSB standards this is "ok" |
| 145 | echo -n " [ OK ] " |
| 146 | fi |
| 147 | echo |
| 148 | } |
| 149 | |
| 150 | restart(){ |
| 151 | stop |
| 152 | start |
| 153 | } |
| 154 | |
| 155 | condrestart(){ |
| 156 | [ -e "$lockfile" ] && restart || : |
| 157 | } |
| 158 | |
| 159 | reload(){ |
| 160 | $SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null |
| 161 | } |
| 162 | |
| 163 | |
| 164 | # See how we were called. |
| 165 | case "$1" in |
| 166 | start) |
| 167 | start |
| 168 | ;; |
| 169 | stop) |
| 170 | stop |
| 171 | ;; |
| 172 | status) |
| 173 | status postmaster |
| 174 | script_result=$? |
| 175 | ;; |
| 176 | restart) |
| 177 | restart |
| 178 | ;; |
| 179 | condrestart|try-restart) |
| 180 | condrestart |
| 181 | ;; |
| 182 | reload) |
| 183 | reload |
| 184 | ;; |
| 185 | force-reload) |
| 186 | restart |
| 187 | ;; |
| 188 | *) |
| 189 | echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" |
| 190 | exit 2 |
| 191 | esac |
| 192 | |
| 193 | exit $script_result |