]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- fix status checking on system with non-english locale, thx to Jérôme Augé <jerome...
[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 cd /
14
15 # Source function library
16 . /etc/rc.d/init.d/functions
17
18 # Get network config
19 . /etc/sysconfig/network
20
21 # Get service config
22 if [ -f /etc/sysconfig/postgresql ]; then
23         . /etc/sysconfig/postgresql
24 else
25         nls "Error: %s not found" /etc/sysconfig/postgresql
26         nls " PostgreSQL can't be run."
27         exit 1
28 fi
29
30 if [ ! "$PG_DB_CLUSTERS" ]; then
31         nls "Error: PG_DB_CLUSTERS not found or is empty"
32         nls " PostgreSQL can't be run."
33         exit 1
34 fi
35
36 # Check that networking is up.
37 if is_yes "${NETWORKING}"; then
38         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
39                 msg_network_down PostgreSQL
40                 exit 1
41         fi
42 else
43         exit 0
44 fi
45
46 action="$1"
47
48 # any db cluster as command line argument?
49 if [ $# -gt 1 ]; then
50         shift
51         # perform action for specified clusters only
52         DB_CLUSTERS="$@"
53 else
54         DB_CLUSTERS="$PG_DB_CLUSTERS"
55 fi
56
57 #
58 # Useful functions.
59 #
60
61 #
62 # check for postgresql status
63 #
64 # arguments:
65 # $1 - db cluster
66 #
67 # sets variables:
68 # PG_STATUS = running | not running
69 # PG_PID    = pid of postmaster process
70 #
71 pgstatus() {
72         PG_STATUS="unknown"
73         PG_PID="unknown"
74         output=`LC_ALL=C TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $1 status"`
75         status=`echo $?`
76
77         if [ $status -eq 1 ]; then
78                 PG_STATUS="not running"
79         elif [ $status -eq 0 ]; then
80                 PG_STATUS="running"
81         # or maybe grab it from postmaster.pid file?
82                 PG_PID=`echo "$output" | sed 's/^pg_ctl:.*PID: \([0-9]\+\).*/\1/' | head -1`
83         fi
84 }
85
86 #
87 # start postgresql and display appropriate messages
88 #
89 # arguments:
90 # $1 - db cluster
91 #
92 pgstart() {
93         msg_starting "PostgreSQL $1"
94         daemon --user postgres /usr/bin/pg_ctl -s -D $1 start
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
118 # See how we were called.
119 # Every action is performed for all given (all configured by default)
120 # db clusters.
121 case "$action" in
122   start)
123         for pgdir in $DB_CLUSTERS; do
124                 pgstatus "$pgdir"
125                 if [ "$PG_STATUS" = "running" ]; then
126                         # pg_ctl status can misinform us about postgresql status
127                         # so let's check if postmaster is really alive
128                         if ps ax | grep -v grep | grep -q "$PG_PID"; then
129                                 msg_already_running "PostgreSQL $pgdir"
130                         else
131                                 # pg_ctl has misinformed us about postgresql status;
132                                 # remove pid file and run postgresql
133                                 msg_not_running "PostgreSQL $pgdir"
134                                 rm -f $pgdir/postmaster.pid
135                                 pgstart "$pgdir"
136                         fi
137                 else
138                         pgstart "$pgdir"
139                 fi
140         done
141         pgsubsys
142         ;;
143   stop)
144         for pgdir in $DB_CLUSTERS; do
145                 pgstatus "$pgdir"
146                 if [ "$PG_STATUS" = "not running" ]; then
147                         msg_not_running "PostgreSQL $pgdir"
148                 else
149                         msg_stopping "PostgreSQL $pgdir"
150                         busy
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                                         fail
157                                 else
158                                         ok
159                                 fi
160                         else
161                                 # postgresql is not really alive; pg_ctl misinformed us
162                                 # about the status
163                                 died
164                         fi
165                         rm -f $pgdir/postmaster.pid
166                 fi
167         done
168         pgsubsys
169         ;;
170   status)
171         for pgdir in $DB_CLUSTERS; do
172                 pgstatus "$pgdir"
173                 if [ "$PG_STATUS" = "running" ]; then
174                         show "PostgreSQL cluster %s, PID %s" "$pgdir" "$PG_PID"
175                         progress "$PG_STATUS"
176                 else
177                         show "PostgreSQL cluster %s" "$pgdir"
178                         progress "$PG_STATUS" "$CFAIL"
179                 fi
180                 echo
181         done
182         ;;
183   restart)
184         $0 stop "$DB_CLUSTERS"
185         $0 start "$DB_CLUSTERS"
186         ;;
187   reload|force-reload)
188         for pgdir in $DB_CLUSTERS; do
189                 pgstatus "$pgdir"
190                 if [ "$PG_STATUS" = "not running" ]; then
191                         msg_not_running "PostgreSQL $pgdir"
192                 else
193                         msg_reloading "PostgreSQL $pgdir"
194                         busy
195                         # is postgresql really alive?
196                         if ps ax | grep -v grep | grep -q "$PG_PID"; then
197                                 TMPDIR=/tmp su postgres -c "/usr/bin/pg_ctl -D $pgdir restart 2>&1 >/dev/null"
198                                         pgstatus "$pgdir"
199                                 if [ "$PG_STATUS" = "running" ]; then
200                                         ok
201                                 else
202                                         fail
203                                 fi
204                         else
205                                 # postgresql died and pg_ctl has misinformed us about
206                                 # the status; remove pid file and start it again
207                                 deltext; died
208                                 rm -f $pgdir/postmaster.pid
209                                 pgstart "$pgdir"
210                         fi
211                 fi
212         done
213         pgsubsys
214         ;;
215   init)
216         nls "Note: this is only simple init action for convenience."
217         nls "If you want some non-standard options, consider using initdb(1)."
218         echo
219         for pgdir in $DB_CLUSTERS; do
220                 if [ -f $pgdir/PG_VERSION ]; then
221                         echo $(nls "Skipping existing cluster %s" "$pgdir")
222                 else
223                         echo $(nls "Initializing cluster %s" "$pgdir")
224                         mkdir -p $pgdir
225                         chmod 700 $pgdir
226                         chown postgres:postgres $pgdir
227                         TMPDIR=/tmp su postgres -s /bin/sh -c "initdb -E UNICODE -D $pgdir"
228                 fi
229         done
230         echo $(nls "REMEMBER to setup password for user \"postgres\"")'!'
231         ;;
232   *)
233         msg_usage "$0 {start|stop|init|restart|reload|force-reload|status}"
234         exit 3
235 esac
236
237 exit 0
This page took 0.317127 seconds and 3 git commands to generate.