2 #-------------------------------------------------------------------------
5 # Start/Stop/Restart/HUP/Report status of postmaster
7 # Copyright (c) 2001-2003, PostgreSQL Global Development Group
11 # $Header: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.sh,v 1.36 2003/08/14 18:56:41 tgl Exp $
13 #-------------------------------------------------------------------------
18 $CMDNAME is a utility to start, stop, restart, reload configuration files,
19 or report the status of a PostgreSQL server.
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]
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.)
37 If the -D option is omitted, the environment variable PGDATA is used.
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
46 Options for stop or restart:
47 -m SHUTDOWN-MODE may be 'smart', 'fast', or 'immediate'
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
54 Report bugs to <pgsql-bugs@postgresql.org>."
57 Try \"$CMDNAME --help\" for more information."
60 # Placed here during build
61 bindir='/home/people/tacyas/Eos/util/I386LINUX/bin'
65 # protect the log file
68 # Check for echo -n vs echo \c
70 if echo '\c' | grep -s c >/dev/null 2>&1
80 # Find out where we're located
82 if echo "$0" | grep '/' > /dev/null 2>&1
84 # explicit dir name given
85 self_path=`echo "$0" | sed 's,/[^/]*$,,'` # (dirname command is not portable)
87 # look for it in PATH ('which' command is not portable)
88 for dir in `echo "$PATH" | sed 's/:/ /g'`
90 # empty entry in path means current dir
91 [ -z "$dir" ] && dir='.'
92 if [ -f "$dir/$CMDNAME" ]
100 # Check if needed programs actually exist in path
101 if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ]; then
103 elif [ -x "$bindir/postmaster" ] && [ -x "$bindir/psql" ]; then
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
112 po_path="$PGPATH/postmaster"
130 echo "pg_ctl (PostgreSQL) $VERSION"
135 # we need to do this so -D datadir shows in ps display
144 logfile=`echo "$1" | sed 's/^-l//'`
150 shutdown_mode=`echo "$1" | sed 's/^-m//'`
170 echo "$CMDNAME: invalid option: $1" 1>&2
190 echo "$CMDNAME: invalid operation mode: $1" 1>&2
198 if [ x"$op" = x"" ];then
199 echo "$CMDNAME: no operation mode specified" 1>&2
204 if [ -z "$PGDATA" ];then
205 echo "$CMDNAME: no database directory specified and environment variable PGDATA unset" 1>&2
210 if [ -z "$wait" ]; then
214 restart) wait=no;; # must wait on shutdown anyhow
219 case "$shutdown_mode" in
230 echo "$CMDNAME: invalid shutdown mode: $1" 1>&2
236 if [ "$op" = "reload" ];then
241 DEFPOSTOPTS=$PGDATA/postmaster.opts.default
242 POSTOPTSFILE=$PGDATA/postmaster.opts
243 PIDFILE=$PGDATA/postmaster.pid
244 CONFFILE=$PGDATA/postgresql.conf
246 if [ "$op" = "status" ];then
247 if [ -f "$PIDFILE" ];then
248 PID=`sed -n 1p $PIDFILE`
249 if [ "$PID" -lt 0 ];then
251 echo "$CMDNAME: postgres is running (PID: $PID)"
253 echo "$CMDNAME: postmaster is running (PID: $PID)"
254 echo "Command line was:"
259 echo "$CMDNAME: postmaster or postgres not running"
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
269 echo "$CMDNAME: cannot restart postmaster; postgres is running (PID: $PID)" 1>&2
270 echo "Please terminate postgres and try again." 1>&2
276 # wait for postmaster to shut down
277 if [ "$wait" = yes -o "$op" = restart ];then
279 $silence_echo $ECHO_N "waiting for postmaster to shut down..."$ECHO_C
283 if [ -f "$PIDFILE" ];then
284 $silence_echo $ECHO_N "."$ECHO_C
286 if [ "$cnt" -gt "$wait_seconds" ];then
287 $silence_echo echo " failed"
288 echo "$CMDNAME: postmaster does not shut down" 1>&2
296 $silence_echo echo "done"
299 if [ "$op" = "reload" ];then
300 $silence_echo echo "postmaster successfully signaled"
302 $silence_echo echo "postmaster successfully shut down"
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
314 fi # stop, restart, reload
316 if [ "$op" = "start" -o "$op" = "restart" ];then
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`
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
331 # if we are in restart mode, then look for postmaster.opts
332 eval set X "`cat $POSTOPTSFILE`"; shift
337 eval set X "$POSTOPTS"; shift
340 if [ -n "$logfile" ]; then
341 "$po_path" ${1+"$@"} ${PGDATAOPTS+$PGDATAOPTS} </dev/null >>$logfile 2>&1 &
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 &
349 # if had an old lockfile, check to see if we were able to start
350 if [ -n "$oldpid" ];then
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
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
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.
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
381 # wait for postmaster to start
382 if [ "$wait" = yes ];then
384 $silence_echo $ECHO_N "waiting for postmaster to start..."$ECHO_C
387 if "$PGPATH/psql" -p $PGPORT -l >/dev/null 2>&1
391 $silence_echo $ECHO_N "."$ECHO_C
393 if [ "$cnt" -gt "$wait_seconds" ];then
394 $silence_echo echo "failed"
395 echo "$CMDNAME: postmaster does not start" 1>&2
401 $silence_echo echo "done"
403 $silence_echo echo "postmaster successfully started"
404 fi # start or restart