]>
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 | # | |
24852107 | 44 | pgstatus () { |
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 | |
59 | # arguments: | |
60 | # $1 - db cluster | |
61 | # | |
62 | pgstart() { | |
63 | msg_starting "PostgreSQL $1" | |
64 | busy | |
65 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $1 start 2>&1 >/dev/null" | |
66 | ||
67 | pgstatus "$1" | |
68 | if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then | |
69 | deltext; ok | |
70 | else | |
71 | deltext; fail | |
72 | fi | |
73 | } | |
74 | ||
24852107 | 75 | action="$1" |
76 | ||
77 | # any db cluster as command line argument? | |
78 | if [ $# -gt 1 ]; then | |
79 | shift | |
80 | POSTGRES_DB_CLUSTERS="$@" | |
81 | fi | |
15e46b8a | 82 | |
d29ea5e3 | 83 | # See how we were called. |
ee1e3deb | 84 | # Every action is performed for all given (default all configured) db clusters. |
24852107 | 85 | case "$action" in |
86 | start) | |
87 | for pgdir in $POSTGRES_DB_CLUSTERS; do | |
88 | pgstatus "$pgdir" | |
89 | if [ "$PG_STATUS" = "running" ]; then | |
ee1e3deb | 90 | # pg_ctl status can misinform us about postgresql status |
91 | # so let's check if postmaster is really alive | |
24852107 | 92 | if ps ax | grep -v grep | grep -q "$PG_PID"; then |
93 | msg_already_running "PostgreSQL $pgdir" | |
94 | else | |
ee1e3deb | 95 | # pg_ctl has misinformed us about postgresql status; |
96 | # remove pid file and run postgresql | |
24852107 | 97 | msg_not_running "PostgreSQL $pgdir" |
98 | rm -f $pgdir/postmaster.pid | |
ee1e3deb | 99 | pgstart "$pgdir" |
24852107 | 100 | fi |
101 | else | |
ee1e3deb | 102 | pgstart "$pgdir" |
2a514bb5 | 103 | fi |
24852107 | 104 | done |
d29ea5e3 | 105 | ;; |
24852107 | 106 | stop) |
107 | for pgdir in $POSTGRES_DB_CLUSTERS; do | |
108 | pgstatus "$pgdir" | |
109 | if [ "$PG_STATUS" = "not running" ]; then | |
110 | msg_not_running "PostgreSQL $pgdir" | |
111 | else | |
112 | msg_stopping "PostgreSQL $pgdir" | |
113 | busy | |
114 | ||
ee1e3deb | 115 | # is postgresql really alive? |
24852107 | 116 | if ps ax | grep -v grep | grep -q "$PG_PID"; then |
117 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -w -D $pgdir stop -m fast 2>&1 >/dev/null" | |
118 | pgstatus "$pgdir" | |
119 | if [ "$PG_STATUS" != "not running" ]; then | |
120 | deltext; fail | |
121 | else | |
122 | deltext; ok | |
123 | fi | |
124 | else | |
ee1e3deb | 125 | # postgresql is not really alive; pg_ctl misinformed us |
126 | # about the status | |
24852107 | 127 | deltext; died |
128 | fi | |
129 | ||
130 | rm -f $pgdir/postmaster.pid | |
131 | fi | |
132 | done | |
d29ea5e3 | 133 | ;; |
24852107 | 134 | status) |
135 | for pgdir in $POSTGRES_DB_CLUSTERS; do | |
136 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status" | |
137 | done | |
d29ea5e3 | 138 | ;; |
24852107 | 139 | restart) |
140 | $0 stop "$POSTGRES_DB_CLUSTERS" | |
141 | $0 start "$POSTGRES_DB_CLUSTERS" | |
962e37bd | 142 | ;; |
24852107 | 143 | reload) |
144 | for pgdir in $POSTGRES_DB_CLUSTERS; do | |
145 | pgstatus "$pgdir" | |
146 | if [ "$PG_STATUS" = "not running" ]; then | |
147 | msg_not_running "PostgreSQL $pgdir" | |
148 | else | |
149 | msg_reloading "PostgreSQL $pgdir" | |
150 | busy | |
151 | ||
ee1e3deb | 152 | # is postgresql really alive? |
24852107 | 153 | if ps ax | grep -v grep | grep -q "$PG_PID"; then |
154 | TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null" | |
155 | ||
156 | pgstatus "$pgdir" | |
157 | if [ "$PG_STATUS" = "running" ]; then | |
158 | deltext; ok | |
159 | else | |
160 | deltext; fail | |
161 | fi | |
162 | else | |
ee1e3deb | 163 | # postgresql died and pg_ctl has misinformed us about |
164 | # the status; remove pid file and start it again | |
24852107 | 165 | deltext; died |
166 | rm -f $pgdir/postmaster.pid | |
ee1e3deb | 167 | pgstart "$pgdir" |
24852107 | 168 | fi |
169 | fi | |
170 | done | |
8d5c7b0d | 171 | ;; |
24852107 | 172 | *) |
173 | msg_usage "$0 {start|stop|status|restart|reload}" | |
174 | exit 1 | |
d29ea5e3 | 175 | esac |
176 | ||
177 | exit 0 |