]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- better start
[packages/postgresql.git] / postgresql.init
1 #!/bin/sh
2 #
3 # postgresql    This is the init script for starting up the PostgreSQL
4 #               server
5 #
6 # chkconfig:    345 84 25
7 #
8 # description:  Starts and stops the PostgreSQL backend daemon that handles \
9 #                               all database requests.
10 #
11 # processname:  postmaster
12
13 # Source function library
14 . /etc/rc.d/init.d/functions
15
16 # Get network config
17 . /etc/sysconfig/network
18
19 # Get service config
20 if [ -f /etc/sysconfig/postgresql ]; then
21         . /etc/sysconfig/postgresql
22 else
23         echo "Error: /etc/sysconfig/postgresql not found"
24         echo " PostgreSQL can't be run."
25         exit 1
26 fi
27
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
34 # Check that networking is up.
35 if is_no "${NETWORKING}"; then
36         msg_network_down PostgreSQL
37         exit 1
38 fi
39
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
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 #
65 pgstatus() {
66         PG_STATUS="unknown"
67         PG_PID="unknown"
68         status=`/usr/bin/pg_ctl -D $1 status`
69
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
78 #
79 # start postgresql and display appropriate messages
80 #
81 # arguments:
82 # $1 - db cluster
83 #
84 pgstart() {
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"
88         sleep 1
89         pgstatus "$1"
90         if [ "$PG_STATUS" != "unknown" -a "$PG_PID" != "unknown" ]; then
91                 deltext; ok
92         else
93                 deltext; fail 
94         fi
95 }
96
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 }
112
113 #
114 # End of useful functions.
115 #
116
117 # See how we were called.
118 # Every action is performed for all given (all configured by default)
119 # db clusters.
120 case "$action" in
121         start)
122                 for pgdir in $DB_CLUSTERS; do
123                         pgstatus "$pgdir"
124                         if [ "$PG_STATUS" = "running" ]; then
125                                 # pg_ctl status can misinform us about postgresql status
126                                 # so let's check if postmaster is really alive
127                                 if ps ax | grep -v grep | grep -q "$PG_PID"; then
128                                         msg_already_running "PostgreSQL $pgdir"
129                                 else
130                                         # pg_ctl has misinformed us about postgresql status;
131                                         # remove pid file and run postgresql
132                                         msg_not_running "PostgreSQL $pgdir"
133                                         rm -f $pgdir/postmaster.pid
134                                         pgstart "$pgdir"
135                                 fi
136                         else
137                                 pgstart "$pgdir"
138                         fi
139                 done
140                 pgsubsys
141         ;;
142         stop)
143                 for pgdir in $DB_CLUSTERS; do
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
151                                 # is postgresql really alive?
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
161                                         # postgresql is not really alive; pg_ctl misinformed us
162                                         # about the status
163                                         deltext; died
164                                 fi
165
166                                 rm -f $pgdir/postmaster.pid
167                         fi
168                 done
169                 pgsubsys
170         ;;
171         status)
172                 for pgdir in $DB_CLUSTERS; do
173                         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
174                 done
175         ;;
176         restart)
177                 $0 stop "$DB_CLUSTERS"
178                 $0 start "$DB_CLUSTERS"
179         ;;
180         reload)
181                 for pgdir in $DB_CLUSTERS; do
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
189                                 # is postgresql really alive?
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
200                                         # postgresql died and pg_ctl has misinformed us about
201                                         # the status; remove pid file and start it again
202                                         deltext; died
203                                         rm -f $pgdir/postmaster.pid
204                                         pgstart "$pgdir"
205                                 fi
206                         fi
207                 done
208                 pgsubsys
209         ;;
210         *)
211                 msg_usage "$0 {start|stop|status|restart|reload}"
212                 exit 1
213 esac
214
215 exit 0
This page took 0.052349 seconds and 4 git commands to generate.