]>
Commit | Line | Data |
---|---|---|
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 | 20 | if [ -f /etc/sysconfig/postgresql ]; then |
24852107 | 21 | . /etc/sysconfig/postgresql |
22 | else | |
23 | echo "Error: /etc/sysconfig/postgresql not found" | |
24 | echo " PostgreSQL can't be run." | |
25 | exit 1 | |
03af0381 | 26 | fi |
27 | ||
dafcea45 | 28 | if [ ! "$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 | |
32 | fi | |
33 | ||
c35cc882 | 34 | # Check that networking is up. |
95633833 | 35 | if is_no "${NETWORKING}"; then |
11f320d0 | 36 | msg_network_down PostgreSQL |
d584f4fd | 37 | exit 1 |
38 | fi | |
d29ea5e3 | 39 | |
93ace391 | 40 | action="$1" |
41 | ||
42 | # any db cluster as command line argument? | |
43 | if [ $# -gt 1 ]; then | |
44 | shift | |
45 | # perform action for specified clusters only | |
46 | DB_CLUSTERS="$@" | |
47 | else | |
48 | DB_CLUSTERS="$PG_DB_CLUSTERS" | |
49 | fi | |
50 | ||
51 | # | |
52 | # Useful functions. | |
53 | # | |
54 | ||
ee1e3deb | 55 | # |
56 | # check for postgresql status | |
57 | # | |
58 | # arguments: | |
59 | # $1 - db cluster | |
60 | # | |
61 | # sets variables: | |
62 | # PG_STATUS = running | not running | |
63 | # PG_PID = pid of postmaster process | |
64 | # | |
293d606e | 65 | pgstatus() { |
24852107 | 66 | PG_STATUS="unknown" |
67 | PG_PID="unknown" | |
68 | status=`/usr/bin/pg_ctl -D $1 status` | |
15e46b8a | 69 | |
24852107 | 70 | if echo "$status" | grep -q 'pg_ctl: postmaster or postgres is not running'; then |
71 | PG_STATUS="not running" | |
72 | elif echo "$status" | grep -q 'pg_ctl: postmaster is running'; then | |
73 | PG_STATUS="running" | |
74 | PG_PID=`echo "$status" | sed 's/^pg_ctl:.*pid: \([0-9]\+\).*/\1/' | head -1` | |
75 | fi | |
76 | } | |
77 | ||
ee1e3deb | 78 | # |
79 | # start postgresql and display appropriate messages | |
293d606e | 80 | # |
ee1e3deb | 81 | # arguments: |
82 | # $1 - db cluster | |
83 | # | |
84 | pgstart() { | |
93ace391 | 85 | msg_starting "PostgreSQL $1" |
86 | busy | |
87 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null" | |
fc99439e | 88 | sleep 1 |
93ace391 | 89 | pgstatus "$1" |
90 | if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then | |
91 | deltext; ok | |
92 | else | |
93 | deltext; fail | |
94 | fi | |
ee1e3deb | 95 | } |
96 | ||
93ace391 | 97 | # |
98 | # check for running postgresql instances; if any instance is running then | |
99 | # create subsys lock file | |
100 | # | |
101 | pgsubsys() { | |
102 | # check for every defined db cluster in sysconfig file | |
103 | for pgdir in $PG_DB_CLUSTERS; do | |
104 | pgstatus "$pgdir" | |
105 | if [ "$PG_STATUS" = "running" ]; then | |
106 | touch /var/lock/subsys/postgresql | |
107 | return | |
108 | fi | |
109 | done | |
110 | rm -f /var/lock/subsys/postgresql | |
111 | } | |
24852107 | 112 | |
93ace391 | 113 | # |
114 | # End of useful functions. | |
115 | # | |
15e46b8a | 116 | |
d29ea5e3 | 117 | # See how we were called. |
293d606e | 118 | # Every action is performed for all given (all configured by default) |
119 | # db clusters. | |
24852107 | 120 | case "$action" in |
121 | start) | |
293d606e | 122 | for pgdir in $DB_CLUSTERS; do |
24852107 | 123 | pgstatus "$pgdir" |
124 | if [ "$PG_STATUS" = "running" ]; then | |
93ace391 | 125 | # pg_ctl status can misinform us about postgresql status |
126 | # so let's check if postmaster is really alive | |
24852107 | 127 | if ps ax | grep -v grep | grep -q "$PG_PID"; then |
128 | msg_already_running "PostgreSQL $pgdir" | |
129 | else | |
93ace391 | 130 | # pg_ctl has misinformed us about postgresql status; |
131 | # remove pid file and run postgresql | |
24852107 | 132 | msg_not_running "PostgreSQL $pgdir" |
133 | rm -f $pgdir/postmaster.pid | |
93ace391 | 134 | pgstart "$pgdir" |
24852107 | 135 | fi |
136 | else | |
93ace391 | 137 | pgstart "$pgdir" |
2a514bb5 | 138 | fi |
24852107 | 139 | done |
93ace391 | 140 | pgsubsys |
d29ea5e3 | 141 | ;; |
24852107 | 142 | stop) |
293d606e | 143 | for pgdir in $DB_CLUSTERS; do |
24852107 | 144 | pgstatus "$pgdir" |
145 | if [ "$PG_STATUS" = "not running" ]; then | |
146 | msg_not_running "PostgreSQL $pgdir" | |
147 | else | |
148 | msg_stopping "PostgreSQL $pgdir" | |
149 | busy | |
150 | ||
93ace391 | 151 | # is postgresql really alive? |
24852107 | 152 | if ps ax | grep -v grep | grep -q "$PG_PID"; then |
153 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null" | |
154 | pgstatus "$pgdir" | |
155 | if [ "$PG_STATUS" != "not running" ]; then | |
156 | deltext; fail | |
157 | else | |
158 | deltext; ok | |
159 | fi | |
160 | else | |
93ace391 | 161 | # postgresql is not really alive; pg_ctl misinformed us |
162 | # about the status | |
24852107 | 163 | deltext; died |
164 | fi | |
165 | ||
166 | rm -f $pgdir/postmaster.pid | |
167 | fi | |
168 | done | |
93ace391 | 169 | pgsubsys |
d29ea5e3 | 170 | ;; |
24852107 | 171 | status) |
293d606e | 172 | for pgdir in $DB_CLUSTERS; do |
24852107 | 173 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status" |
93ace391 | 174 | done |
d29ea5e3 | 175 | ;; |
24852107 | 176 | restart) |
293d606e | 177 | $0 stop "$DB_CLUSTERS" |
178 | $0 start "$DB_CLUSTERS" | |
962e37bd | 179 | ;; |
24852107 | 180 | reload) |
293d606e | 181 | for pgdir in $DB_CLUSTERS; do |
24852107 | 182 | pgstatus "$pgdir" |
183 | if [ "$PG_STATUS" = "not running" ]; then | |
184 | msg_not_running "PostgreSQL $pgdir" | |
185 | else | |
186 | msg_reloading "PostgreSQL $pgdir" | |
187 | busy | |
188 | ||
93ace391 | 189 | # is postgresql really alive? |
24852107 | 190 | if ps ax | grep -v grep | grep -q "$PG_PID"; then |
191 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null" | |
192 | ||
193 | pgstatus "$pgdir" | |
194 | if [ "$PG_STATUS" = "running" ]; then | |
195 | deltext; ok | |
196 | else | |
197 | deltext; fail | |
198 | fi | |
199 | else | |
93ace391 | 200 | # postgresql died and pg_ctl has misinformed us about |
201 | # the status; remove pid file and start it again | |
24852107 | 202 | deltext; died |
203 | rm -f $pgdir/postmaster.pid | |
ee1e3deb | 204 | pgstart "$pgdir" |
24852107 | 205 | fi |
206 | fi | |
207 | done | |
93ace391 | 208 | pgsubsys |
8d5c7b0d | 209 | ;; |
24852107 | 210 | *) |
211 | msg_usage "$0 {start|stop|status|restart|reload}" | |
212 | exit 1 | |
d29ea5e3 | 213 | esac |
214 | ||
215 | exit 0 |