]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- more comments
[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 # Check that networking is up.
29 if is_no "${NETWORKING}"; then
30         msg_network_down PostgreSQL
31         exit 1
32 fi
33
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 #
44 pgstatus() {
45         PG_STATUS="unknown"
46         PG_PID="unknown"
47         status=`/usr/bin/pg_ctl -D $1 status`
48
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
57 #
58 # start postgresql and display appropriate messages
59 #
60 # arguments:
61 # $1 - db cluster
62 #
63 # sets variables:
64 # PG_STARTED = 1 if instance has started up; please note, that function
65 #              will not set to other value
66 #
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
74         PG_STARTED=1
75         deltext; ok
76     else
77         deltext; fail 
78     fi
79 }
80
81 action="$1"
82
83 # any db cluster as command line argument?
84 if [ $# -gt 1 ]; then
85         shift
86         DB_CLUSTERS="$@"
87 else
88     DB_CLUSTERS="$PG_DB_CLUSTERS"
89 fi
90
91 # See how we were called.
92 # Every action is performed for all given (all configured by default)
93 # db clusters.
94 case "$action" in
95         start)
96                 PG_STARTED=0
97                 for pgdir in $DB_CLUSTERS; do
98                         pgstatus "$pgdir"
99                         if [ "$PG_STATUS" = "running" ]; then
100                 # pg_ctl status can misinform us about postgresql status
101                 # so let's check if postmaster is really alive
102                                 if ps ax | grep -v grep | grep -q "$PG_PID"; then
103                                         msg_already_running "PostgreSQL $pgdir"
104                                 else
105                     # pg_ctl has misinformed us about postgresql status;
106                     # remove pid file and run postgresql
107                                         msg_not_running "PostgreSQL $pgdir"
108                                         rm -f $pgdir/postmaster.pid
109                     pgstart "$pgdir"
110                                 fi
111                         else
112                 pgstart "$pgdir"
113                         fi
114                 done
115         # one of instances has started, so create the lock
116         [ $PG_STARTED -eq 1 ] && touch /var/lock/subsys/postgresql
117         ;;
118         stop)
119                 for pgdir in $DB_CLUSTERS; do
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
127                 # is postgresql really alive?
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
137                     # postgresql is not really alive; pg_ctl misinformed us
138                     # about the status
139                                         deltext; died
140                                 fi
141
142                                 rm -f $pgdir/postmaster.pid
143                         fi
144                 done
145         ;;
146         status)
147                 for pgdir in $DB_CLUSTERS; do
148                         TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir status"
149                 done
150         ;;
151         restart)
152                 $0 stop "$DB_CLUSTERS"
153                 $0 start "$DB_CLUSTERS"
154         ;;
155         reload)
156                 for pgdir in $DB_CLUSTERS; do
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
164                 # is postgresql really alive?
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
175                     # postgresql died and pg_ctl has misinformed us about
176                     # the status; remove pid file and start it again
177                                         deltext; died
178                                         rm -f $pgdir/postmaster.pid
179                                         pgstart "$pgdir"
180                                 fi
181                         fi
182                 done
183         ;;
184         *)
185                 msg_usage "$0 {start|stop|status|restart|reload}"
186                 exit 1
187 esac
188
189 exit 0
This page took 0.060007 seconds and 4 git commands to generate.