]> git.pld-linux.org Git - packages/postgresql.git/blob - postgresql.init
- added missing changelog entry (sorry guys, my fault)
[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 # 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
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
82
83 # See how we were called.
84 # Every action is performed for all given (default all configured) db clusters.
85 case "$action" in
86         start)
87                 for pgdir in $POSTGRES_DB_CLUSTERS; do
88                         pgstatus "$pgdir"
89                         if [ "$PG_STATUS" = "running" ]; then
90                 # pg_ctl status can misinform us about postgresql status
91                 # so let's check if postmaster is really alive
92                                 if ps ax | grep -v grep | grep -q "$PG_PID"; then
93                                         msg_already_running "PostgreSQL $pgdir"
94                                 else
95                     # pg_ctl has misinformed us about postgresql status;
96                     # remove pid file and run postgresql
97                                         msg_not_running "PostgreSQL $pgdir"
98                                         rm -f $pgdir/postmaster.pid
99                     pgstart "$pgdir"
100                                 fi
101                         else
102                 pgstart "$pgdir"
103                         fi
104                 done
105         ;;
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
115                 # is postgresql really alive?
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
125                     # postgresql is not really alive; pg_ctl misinformed us
126                     # about the status
127                                         deltext; died
128                                 fi
129
130                                 rm -f $pgdir/postmaster.pid
131                         fi
132                 done
133         ;;
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
138         ;;
139         restart)
140                 $0 stop "$POSTGRES_DB_CLUSTERS"
141                 $0 start "$POSTGRES_DB_CLUSTERS"
142         ;;
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
152                 # is postgresql really alive?
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
163                     # postgresql died and pg_ctl has misinformed us about
164                     # the status; remove pid file and start it again
165                                         deltext; died
166                                         rm -f $pgdir/postmaster.pid
167                                         pgstart "$pgdir"
168                                 fi
169                         fi
170                 done
171         ;;
172         *)
173                 msg_usage "$0 {start|stop|status|restart|reload}"
174                 exit 1
175 esac
176
177 exit 0
This page took 0.608472 seconds and 3 git commands to generate.