]> git.pld-linux.org Git - packages/postgresql.git/blame - postgresql.init
- check for existence of PG_DB_CLUSTERS in postgresql.sysconfig
[packages/postgresql.git] / postgresql.init
CommitLineData
c35cc882 1#!/bin/sh
2#
d29ea5e3 3# postgresql This is the init script for starting up the PostgreSQL
4# server
5#
4f7ddd24 6# chkconfig: 345 84 25
c35cc882 7#
8# description: Starts and stops the PostgreSQL backend daemon that handles \
24852107 9# all database requests.
c35cc882 10#
11# processname: postmaster
c35cc882 12
c35cc882 13# Source function library
d29ea5e3 14. /etc/rc.d/init.d/functions
15
c35cc882 16# Get network config
d29ea5e3 17. /etc/sysconfig/network
18
c35cc882 19# Get service config
dafcea45 20if [ -f /etc/sysconfig/postgresql ]; then
24852107 21 . /etc/sysconfig/postgresql
22else
23 echo "Error: /etc/sysconfig/postgresql not found"
24 echo " PostgreSQL can't be run."
25 exit 1
03af0381 26fi
27
dafcea45 28if [ ! "$PG_DB_CLUSTERS" ]; then
29 echo "Error: PG_DB_CLUSTERS not found or is empty"
30 echo " PostgreSQL can't be run."
31 exit 1
32fi
33
c35cc882 34# Check that networking is up.
95633833 35if is_no "${NETWORKING}"; then
11f320d0 36 msg_network_down PostgreSQL
d584f4fd 37 exit 1
38fi
d29ea5e3 39
ee1e3deb 40#
41# check for postgresql status
42#
43# arguments:
44# $1 - db cluster
45#
46# sets variables:
47# PG_STATUS = running | not running
48# PG_PID = pid of postmaster process
49#
293d606e 50pgstatus() {
24852107 51 PG_STATUS="unknown"
52 PG_PID="unknown"
53 status=`/usr/bin/pg_ctl -D $1 status`
15e46b8a 54
24852107 55 if echo "$status" | grep -q 'pg_ctl: postmaster or postgres is not running'; then
56 PG_STATUS="not running"
57 elif echo "$status" | grep -q 'pg_ctl: postmaster is running'; then
58 PG_STATUS="running"
59 PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1`
60 fi
61}
62
ee1e3deb 63#
64# start postgresql and display appropriate messages
293d606e 65#
ee1e3deb 66# arguments:
67# $1 - db cluster
68#
293d606e 69# sets variables:
70# PG_STARTED = 1 if instance has started up; please note, that function
71# will not set to other value
72#
ee1e3deb 73pgstart() {
74 msg_starting "PostgreSQL $1"
75 busy
76 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null"
77
78 pgstatus "$1"
79 if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
293d606e 80 PG_STARTED=1
ee1e3deb 81 deltext; ok
82 else
83 deltext; fail
84 fi
85}
86
24852107 87action="$1"
88
89# any db cluster as command line argument?
90if [ $# -gt 1 ]; then
91 shift
293d606e 92 DB_CLUSTERS="$@"
93else
94 DB_CLUSTERS="$PG_DB_CLUSTERS"
24852107 95fi
15e46b8a 96
d29ea5e3 97# See how we were called.
293d606e 98# Every action is performed for all given (all configured by default)
99# db clusters.
24852107 100case "$action" in
101 start)
293d606e 102 PG_STARTED=0
103 for pgdir in $DB_CLUSTERS; do
24852107 104 pgstatus "$pgdir"
105 if [ "$PG_STATUS" = "running" ]; then
ee1e3deb 106 # pg_ctl status can misinform us about postgresql status
107 # so let's check if postmaster is really alive
24852107 108 if ps ax | grep -v grep | grep -q "$PG_PID"; then
109 msg_already_running "PostgreSQL $pgdir"
110 else
ee1e3deb 111 # pg_ctl has misinformed us about postgresql status;
112 # remove pid file and run postgresql
24852107 113 msg_not_running "PostgreSQL $pgdir"
114 rm -f $pgdir/postmaster.pid
ee1e3deb 115 pgstart "$pgdir"
24852107 116 fi
117 else
ee1e3deb 118 pgstart "$pgdir"
2a514bb5 119 fi
24852107 120 done
293d606e 121 # one of instances has started, so create the lock
122 [ $PG_STARTED -eq 1 ] && touch /var/lock/subsys/postgresql
d29ea5e3 123 ;;
24852107 124 stop)
293d606e 125 for pgdir in $DB_CLUSTERS; do
24852107 126 pgstatus "$pgdir"
127 if [ "$PG_STATUS" = "not running" ]; then
128 msg_not_running "PostgreSQL $pgdir"
129 else
130 msg_stopping "PostgreSQL $pgdir"
131 busy
132
ee1e3deb 133 # is postgresql really alive?
24852107 134 if ps ax | grep -v grep | grep -q "$PG_PID"; then
135 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null"
136 pgstatus "$pgdir"
137 if [ "$PG_STATUS" != "not running" ]; then
138 deltext; fail
139 else
140 deltext; ok
141 fi
142 else
ee1e3deb 143 # postgresql is not really alive; pg_ctl misinformed us
144 # about the status
24852107 145 deltext; died
146 fi
147
148 rm -f $pgdir/postmaster.pid
149 fi
150 done
d29ea5e3 151 ;;
24852107 152 status)
293d606e 153 for pgdir in $DB_CLUSTERS; do
24852107 154 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
155 done
d29ea5e3 156 ;;
24852107 157 restart)
293d606e 158 $0 stop "$DB_CLUSTERS"
159 $0 start "$DB_CLUSTERS"
962e37bd 160 ;;
24852107 161 reload)
293d606e 162 for pgdir in $DB_CLUSTERS; do
24852107 163 pgstatus "$pgdir"
164 if [ "$PG_STATUS" = "not running" ]; then
165 msg_not_running "PostgreSQL $pgdir"
166 else
167 msg_reloading "PostgreSQL $pgdir"
168 busy
169
ee1e3deb 170 # is postgresql really alive?
24852107 171 if ps ax | grep -v grep | grep -q "$PG_PID"; then
172 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
173
174 pgstatus "$pgdir"
175 if [ "$PG_STATUS" = "running" ]; then
176 deltext; ok
177 else
178 deltext; fail
179 fi
180 else
ee1e3deb 181 # postgresql died and pg_ctl has misinformed us about
182 # the status; remove pid file and start it again
24852107 183 deltext; died
184 rm -f $pgdir/postmaster.pid
ee1e3deb 185 pgstart "$pgdir"
24852107 186 fi
187 fi
188 done
8d5c7b0d 189 ;;
24852107 190 *)
191 msg_usage "$0 {start|stop|status|restart|reload}"
192 exit 1
d29ea5e3 193esac
194
195exit 0
This page took 0.113639 seconds and 4 git commands to generate.