OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / bin / pg_ctl
1 #! /bin/sh
2 #-------------------------------------------------------------------------
3 #
4 # pg_ctl.sh--
5 #    Start/Stop/Restart/HUP/Report status of postmaster
6 #
7 # Copyright (c) 2001-2003, PostgreSQL Global Development Group
8 #
9 #
10 # IDENTIFICATION
11 #    $Header: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.sh,v 1.36 2003/08/14 18:56:41 tgl Exp $
12 #
13 #-------------------------------------------------------------------------
14
15 CMDNAME=`basename $0`
16
17 help="\
18 $CMDNAME is a utility to start, stop, restart, reload configuration files,
19 or report the status of a PostgreSQL server.
20
21 Usage:
22   $CMDNAME start   [-w] [-D DATADIR] [-s] [-l FILENAME] [-o \"OPTIONS\"]
23   $CMDNAME stop    [-W] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
24   $CMDNAME restart [-w] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o \"OPTIONS\"]
25   $CMDNAME reload  [-D DATADIR] [-s]
26   $CMDNAME status  [-D DATADIR]
27
28 Common options:
29   -D DATADIR   location of the database storage area
30   -s           only print errors, no informational messages
31   -w           wait until operation completes
32   -W           do not wait until operation completes
33   --help       show this help, then exit
34   --version    output version information, then exit
35 (The default is to wait for shutdown, but not for start or restart.)
36
37 If the -D option is omitted, the environment variable PGDATA is used.
38
39 Options for start or restart:
40   -l FILENAME             write (or append) server log to FILENAME.  The
41                           use of this option is highly recommended.
42   -o OPTIONS              command line options to pass to the postmaster
43                           (PostgreSQL server executable)
44   -p PATH-TO-POSTMASTER   normally not necessary
45
46 Options for stop or restart:
47   -m SHUTDOWN-MODE   may be 'smart', 'fast', or 'immediate'
48
49 Shutdown modes are:
50   smart       quit after all clients have disconnected
51   fast        quit directly, with proper shutdown
52   immediate   quit without complete shutdown; will lead to recovery on restart
53
54 Report bugs to <pgsql-bugs@postgresql.org>."
55
56 advice="\
57 Try \"$CMDNAME --help\" for more information."
58
59
60 # Placed here during build
61 bindir='/home/people/tacyas/Eos/util/I386LINUX/bin'
62 VERSION='7.4.1'
63 DEF_PGPORT='5432'
64
65 # protect the log file
66 umask 077
67
68 # Check for echo -n vs echo \c
69
70 if echo '\c' | grep -s c >/dev/null 2>&1
71 then
72     ECHO_N="echo -n"
73     ECHO_C=""
74 else
75     ECHO_N="echo"
76     ECHO_C='\c'
77 fi
78
79 #
80 # Find out where we're located
81 #
82 if echo "$0" | grep '/' > /dev/null 2>&1 
83 then
84         # explicit dir name given
85         self_path=`echo "$0" | sed 's,/[^/]*$,,'`       # (dirname command is not portable)
86 else
87         # look for it in PATH ('which' command is not portable)
88         for dir in `echo "$PATH" | sed 's/:/ /g'`
89         do
90                 # empty entry in path means current dir
91                 [ -z "$dir" ] && dir='.'
92                 if [ -f "$dir/$CMDNAME" ]
93                 then
94                         self_path="$dir"
95                         break
96                 fi
97         done
98 fi
99
100 # Check if needed programs actually exist in path
101 if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ]; then
102     PGPATH="$self_path"
103 elif [ -x "$bindir/postmaster" ] && [ -x "$bindir/psql" ]; then
104     PGPATH="$bindir"
105 else
106     echo "The programs \"postmaster\" and \"psql\" are needed by $CMDNAME but" 1>&2
107     echo "were not found in the directory \"$bindir\"." 1>&2
108     echo "Check your installation." 1>&2
109     exit 1
110 fi
111
112 po_path="$PGPATH/postmaster"
113
114 wait=
115 wait_seconds=60
116 logfile=
117 silence_echo=
118 shutdown_mode=smart
119 PGDATAOPTS=""
120 POSTOPTS=""
121
122 while [ "$#" -gt 0 ]
123 do
124     case "$1" in
125         -h|--help|-\?)
126             echo "$help"
127             exit 0
128             ;;
129         -V|--version)
130             echo "pg_ctl (PostgreSQL) $VERSION"
131             exit 0
132             ;;
133         -D)
134             shift
135             # we need to do this so -D datadir shows in ps display
136             PGDATAOPTS="-D $1"
137             PGDATA="$1"
138             export PGDATA
139             ;;
140         -l)
141             logfile="$2"
142             shift;;
143         -l*)
144             logfile=`echo "$1" | sed 's/^-l//'`
145             ;;
146         -m)
147             shutdown_mode="$2"
148             shift;;
149         -m*)
150             shutdown_mode=`echo "$1" | sed 's/^-m//'`
151             ;;
152         -o)
153             shift
154             POSTOPTS="$1"
155             ;;
156         -p)
157             shift
158             po_path="$1"
159             ;;
160         -s)
161             silence_echo=:
162             ;;
163         -w)
164             wait=yes
165             ;;
166         -W)
167             wait=no
168             ;;
169         -*)
170             echo "$CMDNAME: invalid option: $1" 1>&2
171             echo "$advice" 1>&2
172             exit 1
173             ;;
174         start)
175             op="start"
176             ;;
177         stop)
178             op="stop"
179             ;;
180         restart)
181             op="restart"
182             ;;
183         reload)
184             op="reload"
185             ;;
186         status)
187             op="status"
188             ;;
189         *)
190             echo "$CMDNAME: invalid operation mode: $1" 1>&2
191             echo "$advice" 1>&2
192             exit 1
193             ;;
194     esac
195     shift
196 done
197
198 if [ x"$op" = x"" ];then
199     echo "$CMDNAME: no operation mode specified" 1>&2
200     echo "$advice" 1>&2
201     exit 1
202 fi
203
204 if [ -z "$PGDATA" ];then
205     echo "$CMDNAME: no database directory specified and environment variable PGDATA unset" 1>&2
206     echo "$advice" 1>&2
207     exit 1
208 fi
209
210 if [ -z "$wait" ]; then
211     case "$op" in
212         start)      wait=no;;
213         stop)       wait=yes;;
214         restart)    wait=no;;   # must wait on shutdown anyhow
215     esac
216 fi
217
218
219 case "$shutdown_mode" in
220     s|smart)
221         sig="-TERM"
222         ;;
223     f|fast)
224         sig="-INT"
225         ;;
226     i|immediate)
227         sig="-QUIT"
228         ;;
229     *)
230         echo "$CMDNAME: invalid shutdown mode: $1" 1>&2
231         echo "$advice" 1>&2
232         exit 1
233         ;;
234 esac
235
236 if [ "$op" = "reload" ];then
237         sig="-HUP"
238         wait=no
239 fi
240
241 DEFPOSTOPTS=$PGDATA/postmaster.opts.default
242 POSTOPTSFILE=$PGDATA/postmaster.opts
243 PIDFILE=$PGDATA/postmaster.pid
244 CONFFILE=$PGDATA/postgresql.conf
245
246 if [ "$op" = "status" ];then
247     if [ -f "$PIDFILE" ];then
248         PID=`sed -n 1p $PIDFILE`
249         if [ "$PID" -lt 0 ];then
250             PID=`expr 0 - $PID`
251             echo "$CMDNAME: postgres is running (PID: $PID)"
252         else
253             echo "$CMDNAME: postmaster is running (PID: $PID)"
254             echo "Command line was:"
255             cat "$POSTOPTSFILE"
256         fi
257         exit 0
258     else
259         echo "$CMDNAME: postmaster or postgres not running"
260         exit 1
261     fi
262 fi
263
264 if [ "$op" = "stop" -o "$op" = "restart" -o "$op" = "reload" ];then
265     if [ -f "$PIDFILE" ];then
266         PID=`sed -n 1p $PIDFILE`
267         if [ "$PID" -lt 0 ];then
268             PID=`expr 0 - $PID`
269             echo "$CMDNAME: cannot restart postmaster; postgres is running (PID: $PID)" 1>&2
270             echo "Please terminate postgres and try again." 1>&2
271             exit 1
272         fi
273
274         kill "$sig" $PID
275
276         # wait for postmaster to shut down
277         if [ "$wait" = yes -o "$op" = restart ];then
278             cnt=0
279             $silence_echo $ECHO_N "waiting for postmaster to shut down..."$ECHO_C
280
281             while :
282             do
283                 if [ -f "$PIDFILE" ];then
284                     $silence_echo $ECHO_N "."$ECHO_C
285                     cnt=`expr $cnt + 1`
286                     if [ "$cnt" -gt "$wait_seconds" ];then
287                         $silence_echo echo " failed"
288                         echo "$CMDNAME: postmaster does not shut down" 1>&2
289                         exit 1
290                     fi
291                 else
292                     break
293                 fi
294                 sleep 1
295             done
296             $silence_echo echo "done"
297         fi
298
299         if [ "$op" = "reload" ];then
300             $silence_echo echo "postmaster successfully signaled"
301         else
302             $silence_echo echo "postmaster successfully shut down"
303         fi
304
305     else # ! -f $PIDFILE
306         echo "$CMDNAME: could not find $PIDFILE" 1>&2
307         echo "Is postmaster running?" 1>&2
308         if [ "$op" = "restart" ];then
309             echo "starting postmaster anyway" 1>&2
310         else
311             exit 1
312         fi
313     fi
314 fi # stop, restart, reload
315
316 if [ "$op" = "start" -o "$op" = "restart" ];then
317     oldpid=""
318     if [ -f "$PIDFILE" ];then
319         echo "$CMDNAME: Another postmaster may be running.  Trying to start postmaster anyway." 1>&2
320         oldpid=`sed -n 1p $PIDFILE`
321     fi
322
323     # no -o given
324     if [ -z "$POSTOPTS" ];then
325         if [ "$op" = "start" ];then
326             # if we are in start mode, then look for postmaster.opts.default
327             if [ -f "$DEFPOSTOPTS" ]; then
328                 eval set X "`cat $DEFPOSTOPTS`"; shift
329             fi
330         else
331             # if we are in restart mode, then look for postmaster.opts
332             eval set X "`cat $POSTOPTSFILE`"; shift
333             po_path="$1"
334             shift
335         fi
336     else # -o given
337         eval set X "$POSTOPTS"; shift
338     fi
339
340     if [ -n "$logfile" ]; then
341         "$po_path" ${1+"$@"} ${PGDATAOPTS+$PGDATAOPTS} </dev/null >>$logfile 2>&1 &
342     else
343         # when starting without log file, redirect stderr to stdout, so
344         # pg_ctl can be invoked with >$logfile and still have pg_ctl's
345         # stderr on the terminal.
346         "$po_path" ${1+"$@"} ${PGDATAOPTS+$PGDATAOPTS} </dev/null 2>&1 &
347     fi
348
349     # if had an old lockfile, check to see if we were able to start
350     if [ -n "$oldpid" ];then
351         sleep 1
352         if [ -f "$PIDFILE" ];then
353             if [ "`sed -n 1p $PIDFILE`" = "$oldpid" ];then
354                 echo "$CMDNAME: cannot start postmaster" 1>&2
355                 echo "Examine the log output." 1>&2
356                 exit 1
357             fi
358         fi
359     fi
360
361 # FIXME:  This is horribly misconceived.
362 # 1) If password authentication is set up, the connection will fail.
363 # 2) If a virtual host is set up, the connection may fail.
364 # 3) If network traffic filters are set up tight enough, the connection
365 #    may fail.
366 # 4) When no Unix domain sockets are available, the connection will
367 #    fail.  (Using TCP/IP by default ain't better.)
368 # 5) If the dynamic loader is not set up correctly (for this user/at
369 #    this time), psql will fail (to find libpq).
370 # 6) If psql is misconfigured, this may fail.
371
372     # Attempt to use the right port
373     # Use PGPORT if set, otherwise look in the configuration file
374     if [ -z "$PGPORT" ];then
375         PGPORT=`sed -ne 's/^[   ]*port[^=]*=[   ]\+\([0-9]\+\).*/\1/p' $CONFFILE 2>/dev/null`
376         if [ -z "$PGPORT" ];then
377             PGPORT="$DEF_PGPORT"
378         fi
379     fi
380
381     # wait for postmaster to start
382     if [ "$wait" = yes ];then
383         cnt=0
384         $silence_echo $ECHO_N "waiting for postmaster to start..."$ECHO_C
385         while :
386         do
387             if "$PGPATH/psql" -p $PGPORT -l >/dev/null 2>&1
388             then
389                 break;
390             else
391                 $silence_echo $ECHO_N "."$ECHO_C
392                 cnt=`expr $cnt + 1`
393                 if [ "$cnt" -gt "$wait_seconds" ];then
394                     $silence_echo echo "failed"
395                     echo "$CMDNAME: postmaster does not start" 1>&2
396                     exit 1
397                 fi
398                 sleep 1
399             fi
400         done
401         $silence_echo echo "done"
402     fi
403     $silence_echo echo "postmaster successfully started"
404 fi # start or restart
405
406 exit 0