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