Commit 088c0b95 authored by Peter Eisentraut's avatar Peter Eisentraut

Make -w the default for shut down, add -W option to specify no wait.

Add -l option to name log file.  Set umask to 077.
Proper file descriptor redirection to allow postmaster to detach from
shell's process group.
Add -s option to turn off informational messages.
parent e58775ee
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_ctl-ref.sgml,v 1.5 2000/12/25 23:15:26 petere Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_ctl-ref.sgml,v 1.6 2001/02/08 19:39:24 petere Exp $
Postgres documentation Postgres documentation
--> -->
<refentry id="app-pg-ctl"> <refentry id="app-pg-ctl">
<docinfo> <docinfo>
<date>2000-11-25</date> <date>2001-02-08</date>
</docinfo> </docinfo>
<refmeta> <refmeta>
...@@ -25,12 +25,13 @@ Postgres documentation ...@@ -25,12 +25,13 @@ Postgres documentation
<arg choice="plain">start</arg> <arg choice="plain">start</arg>
<arg>-w</arg> <arg>-w</arg>
<arg>-D <replaceable>datadir</replaceable></arg> <arg>-D <replaceable>datadir</replaceable></arg>
<arg>-p <replaceable>path</replaceable></arg> <arg>-l <replaceable>filename</replaceable></arg>
<arg>-o <replaceable>options</replaceable></arg> <arg>-o <replaceable>options</replaceable></arg>
<arg>-p <replaceable>path</replaceable></arg>
<sbr> <sbr>
<command>pg_ctl</command> <command>pg_ctl</command>
<arg choice="plain">stop</arg> <arg choice="plain">stop</arg>
<arg>-w</arg> <arg>-W</arg>
<arg>-D <replaceable>datadir</replaceable></arg> <arg>-D <replaceable>datadir</replaceable></arg>
<arg>-m <arg>-m
<group choice="plain"> <group choice="plain">
...@@ -64,8 +65,52 @@ Postgres documentation ...@@ -64,8 +65,52 @@ Postgres documentation
<title>Description</title> <title>Description</title>
<para> <para>
<application>pg_ctl</application> is a utility for starting, <application>pg_ctl</application> is a utility for starting,
stopping, or restarting the <xref linkend="app-postmaster">, or stopping, or restarting <xref linkend="app-postmaster">, the
displaying the status of a running postmaster. <productname>PostgreSQL</productname> backend server, or displaying
the status of a running postmaster. Although the postmaster can be
started manually, <application>pg_ctl</application> encapulates
tasks such as redirecting log output, properly detaching from the
terminal and process group, and additionally provides an option for
controlled shut down.
</para>
<para>
In <option>start</option> mode, a new postmaster is launched. The
server is started in the background, the standard input attached to
<filename>/dev/null</filename>. The standard output and standard
error are either appended to a log file, if the <option>-l</option>
option is used, or are redirected to
<application>pg_ctl</application>'s standard output (not standard
error). If no log file is chosen, the standard output of
<application>pg_ctl</application> should be redirected to a file or
piped to another process, for example a log rotating program,
otherwise the postmaster will write its output the the controlling
terminal (from the background) and will not leave the shell's
process group.
</para>
<para>
In <option>stop</option> mode, the postmaster that is running on
the specified data directory is shut down. Three different
shutdown methods can be selected with the <option>-m</option>
option: <quote>Smart</quote> mode waits for all the clients to
disconnect. This is the default. <quote>Fast</quote> mode does
not wait for clients to disconnect. All active transactions will
be rolled back. <quote>Immediate</quote> mode will abort without
complete shutdown. This will lead to a recovery run on restart.
By the default, stop mode waits for the shutdown to complete.
</para>
<para>
<option>restart</option> mode effectively executes a stop followed
by a start. This allows the changing of postmaster command line
options.
</para>
<para>
<option>status</option> mode checks whether a postmaster is running
and if so displays the <acronym>PID</acronym> and the command line
options that were used to invoke it.
</para> </para>
<refsect2 id="app-pg-ctl-options"> <refsect2 id="app-pg-ctl-options">
...@@ -74,38 +119,36 @@ Postgres documentation ...@@ -74,38 +119,36 @@ Postgres documentation
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term>-w</term> <term>-D <replaceable class="parameter">datadir</replaceable></term>
<listitem> <listitem>
<para> <para>
Wait for the database server to come up, by watching for Specifies the file system location of the database files. If
creation of the pid file this is omitted, the environment variable
(<filename><replaceable>PGDATA</replaceable>/postmaster.pid</filename>). <envar>PGDATA</envar> is used.
Times out after 60 seconds.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term>-D <replaceable class="parameter">datadir</replaceable></term> <term>-l <replaceable class="parameter">filename</replaceable></term>
<listitem> <listitem>
<para> <para>
Specifies the file system location of the database files. If Append the server log output to
this is omitted, the environment variable <replaceable>filename</replaceable>. If the file does not
<envar>PGDATA</envar> is used. exist, it is created. The umask is set to 077, so access to
the log file from other users is disallowed by default.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term>-p <replaceable class="parameter">path</replaceable></term> <term>-m <replaceable class="parameter">mode</replaceable></term>
<listitem> <listitem>
<para> <para>
Specifies the location of the <filename>postmaster</filename> Specifies the shutdown mode. <replaceable>mode</replaceable>
executable. By default the postmaster is taken from the same may be <literal>smart</literal>, <literal>fast</literal>, or
directory as pg_ctl, or failing that, the hard-wired <literal>immediate</literal>, or the first letter of one of
installation directory. It is not necessary to use this these three.
option unless you are doing something unusual and get errors
that the postmaster was not found.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -125,82 +168,35 @@ Postgres documentation ...@@ -125,82 +168,35 @@ Postgres documentation
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term>-m <replaceable class="parameter">mode</replaceable></term> <term>-p <replaceable class="parameter">path</replaceable></term>
<listitem>
<para>
Specifies the shutdown mode.
<variablelist>
<varlistentry>
<term>smart</term>
<term>s</term>
<listitem>
<para>
Smart mode waits for all the clients to disconnect. This
is the default.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>fast</term>
<term>f</term>
<listitem>
<para>
Fast mode does not wait for clients to disconnect. All
active transactions will be rolled back.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>immediate</term>
<term>i</term>
<listitem>
<para>
Immediate mode will abort without complete shutdown. This
will lead to a recovery run on restart.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>start</term>
<listitem>
<para>
Start up <application>postmaster</application>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>stop</term>
<listitem> <listitem>
<para> <para>
Shut down <application>postmaster</application>. Specifies the location of the <filename>postmaster</filename>
executable. By default the postmaster is taken from the same
directory as pg_ctl, or failing that, the hard-wired
installation directory. It is not necessary to use this
option unless you are doing something unusual and get errors
that the postmaster was not found.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term>restart</term> <term>-w</term>
<listitem> <listitem>
<para> <para>
Stop the <application>postmaster</application>, if one is running, Wait for the start or stutdown to complete. Times out after
and then start it again. 60 seconds. This is the default for shutdowns.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term>status</term> <term>-W</term>
<listitem> <listitem>
<para> <para>
Show the current state of <application>postmaster</application>. Do not wait for start or shutdown to complete. This is the
default for starts and restarts.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -223,7 +219,7 @@ Postgres documentation ...@@ -223,7 +219,7 @@ Postgres documentation
<refsect1 id="R1-APP-PGCTL-2"> <refsect1 id="R1-APP-PGCTL-2">
<title>Usage</title> <title>Examples</title>
<refsect2 id="R2-APP-PGCTL-3"> <refsect2 id="R2-APP-PGCTL-3">
<title>Starting the postmaster</title> <title>Starting the postmaster</title>
...@@ -260,7 +256,6 @@ Postgres documentation ...@@ -260,7 +256,6 @@ Postgres documentation
</screen> </screen>
stops postmaster. Using the <option>-m</option> switch allows one stops postmaster. Using the <option>-m</option> switch allows one
to control <emphasis>how</emphasis> the backend shuts down. to control <emphasis>how</emphasis> the backend shuts down.
<option>-w</option> waits for postmaster to shut down.
</para> </para>
</refsect2> </refsect2>
...@@ -312,6 +307,25 @@ Command line was: ...@@ -312,6 +307,25 @@ Command line was:
</para> </para>
</refsect2> </refsect2>
</refsect1> </refsect1>
<refsect1>
<title>Bugs</title>
<para>
Waiting for complete start is not a well-defined operation and may
fail if access control is set up in way that a local client cannot
connect without manual interaction. It should be avoided.
</para>
</refsect1>
<refsect1>
<title>See Also</title>
<para>
<xref linkend="app-postmaster">, <citetitle>PostgreSQL Administrator's Guide</citetitle>
</para>
</refsect1>
</refentry> </refentry>
<!-- Keep this comment at the end of the file <!-- Keep this comment at the end of the file
......
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
# pg_ctl.sh-- # pg_ctl.sh--
# Start/Stop/Restart/Report status of postmaster # Start/Stop/Restart/Report status of postmaster
# #
# Copyright (c) 2000, PostgreSQL Global Development Group # Copyright (c) 2001 PostgreSQL Global Development Group
# #
# #
# IDENTIFICATION # IDENTIFICATION
# $Header: /cvsroot/pgsql/src/bin/pg_ctl/Attic/pg_ctl.sh,v 1.17 2000/12/30 06:10:43 ishii Exp $ # $Header: /cvsroot/pgsql/src/bin/pg_ctl/Attic/pg_ctl.sh,v 1.18 2001/02/08 19:39:24 petere Exp $
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
...@@ -19,20 +19,29 @@ $CMDNAME is a utility to start, stop, restart, and report the status ...@@ -19,20 +19,29 @@ $CMDNAME is a utility to start, stop, restart, and report the status
of a PostgreSQL server. of a PostgreSQL server.
Usage: Usage:
$CMDNAME start [-w] [-D DATADIR] [-p PATH-TO-POSTMASTER] [-o \"OPTIONS\"] $CMDNAME start [-w] [-D DATADIR] [-s] [-l FILENAME] [-o \"OPTIONS\"]
$CMDNAME stop [-w] [-D DATADIR] [-m SHUTDOWN-MODE] $CMDNAME stop [-W] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
$CMDNAME restart [-w] [-D DATADIR] [-m SHUTDOWN-MODE] [-o \"OPTIONS\"] $CMDNAME restart [-w] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o \"OPTIONS\"]
$CMDNAME status [-D DATADIR] $CMDNAME status [-D DATADIR]
Options: Common options:
-D DATADIR Location of the database storage area -D DATADIR Location of the database storage area
-m SHUTDOWN-MODE May be 'smart', 'fast', or 'immediate' -s Only print errors, no informational messages
-w Wait until operation completes
-W Do not wait until operation completes
(The default is to wait for shutdown, but not for start or restart.)
If the -D option is omitted, the environment variable PGDATA is used.
Options for start or restart:
-l FILENAME Write (or append) server log to FILENAME. The
use of this option is highly recommended.
-o OPTIONS Command line options to pass to the postmaster -o OPTIONS Command line options to pass to the postmaster
(PostgreSQL server executable) (PostgreSQL server executable)
-p PATH-TO-POSTMASTER Normally not necessary -p PATH-TO-POSTMASTER Normally not necessary
-w Wait until operation completes
If the -D option is omitted, the environment variable PGDATA is used. Options for stop or restart:
-m SHUTDOWN-MODE May be 'smart', 'fast', or 'immediate'
Shutdown modes are: Shutdown modes are:
smart Quit after all clients have disconnected smart Quit after all clients have disconnected
...@@ -50,6 +59,9 @@ Try '$CMDNAME --help' for more information." ...@@ -50,6 +59,9 @@ Try '$CMDNAME --help' for more information."
bindir='@bindir@' bindir='@bindir@'
VERSION='@VERSION@' VERSION='@VERSION@'
# protect the log file
umask 077
# Check for echo -n vs echo \c # Check for echo -n vs echo \c
if echo '\c' | grep -s c >/dev/null 2>&1 if echo '\c' | grep -s c >/dev/null 2>&1
...@@ -96,8 +108,11 @@ fi ...@@ -96,8 +108,11 @@ fi
po_path="$PGPATH/postmaster" po_path="$PGPATH/postmaster"
# set default shutdown signal wait=
sig="-TERM" wait_seconds=60
logfile=
silence_echo=
shutdown_mode=smart
while [ "$#" -gt 0 ] while [ "$#" -gt 0 ]
do do
...@@ -114,34 +129,34 @@ do ...@@ -114,34 +129,34 @@ do
shift shift
PGDATA="$1" PGDATA="$1"
;; ;;
-l)
logfile=$2
shift;;
-l*)
logfile=`echo "$1" | sed 's/^-l//'`
;;
-m)
shutdown_mode=$2
shift;;
-m*)
shutdown_mode=`echo "$1" | sed 's/^-m//'`
;;
-o)
shift
POSTOPTS="$1"
;;
-p) -p)
shift shift
po_path="$1" po_path="$1"
;; ;;
-m) -s)
shift silence_echo=:
case $1 in
s|smart)
;;
f|fast)
sig="-INT"
;;
i|immediate)
sig="-QUIT"
;;
*)
echo "$CMDNAME: wrong shutdown mode: $1" 1>&2
echo "$advice" 1>&2
exit 1
;;
esac
;; ;;
-w) -w)
wait=1 wait=yes
;; ;;
-o) -W)
shift wait=no
POSTOPTS="$1"
;; ;;
-*) -*)
echo "$CMDNAME: invalid option: $1" 1>&2 echo "$CMDNAME: invalid option: $1" 1>&2
...@@ -181,20 +196,47 @@ if [ -z "$PGDATA" ];then ...@@ -181,20 +196,47 @@ if [ -z "$PGDATA" ];then
exit 1 exit 1
fi fi
if [ -z "$wait" ]; then
case $op in
start) wait=no;;
stop) wait=yes;;
restart) wait=no;; # must wait on shutdown anyhow
esac
fi
case $shutdown_mode in
s|smart)
sig="-TERM"
;;
f|fast)
sig="-INT"
;;
i|immediate)
sig="-QUIT"
;;
*)
echo "$CMDNAME: invalid shutdown mode: $1" 1>&2
echo "$advice" 1>&2
exit 1
;;
esac
DEFPOSTOPTS=$PGDATA/postmaster.opts.default DEFPOSTOPTS=$PGDATA/postmaster.opts.default
POSTOPTSFILE=$PGDATA/postmaster.opts POSTOPTSFILE=$PGDATA/postmaster.opts
PIDFILE=$PGDATA/postmaster.pid PIDFILE=$PGDATA/postmaster.pid
if [ $op = "status" ];then if [ $op = "status" ];then
if [ -f $PIDFILE ];then if [ -f $PIDFILE ];then
PID=`head -1 $PIDFILE` PID=`sed -n 1p $PIDFILE`
if [ $PID -lt 0 ];then if [ $PID -lt 0 ];then
PID=`expr 0 - $PID` PID=`expr 0 - $PID`
echo "$CMDNAME: postgres is running (pid: $PID)" echo "$CMDNAME: postgres is running (pid: $PID)"
else else
echo "$CMDNAME: postmaster is running (pid: $PID)" echo "$CMDNAME: postmaster is running (pid: $PID)"
echo "Command line was:" echo "Command line was:"
echo "`cat $POSTOPTSFILE`" cat "$POSTOPTSFILE"
fi fi
exit 0 exit 0
else else
...@@ -205,29 +247,29 @@ fi ...@@ -205,29 +247,29 @@ fi
if [ $op = "stop" -o $op = "restart" ];then if [ $op = "stop" -o $op = "restart" ];then
if [ -f $PIDFILE ];then if [ -f $PIDFILE ];then
PID=`head -1 $PIDFILE` PID=`sed -n 1p $PIDFILE`
if [ $PID -lt 0 ];then if [ $PID -lt 0 ];then
PID=`expr 0 - $PID` PID=`expr 0 - $PID`
echo "$CMDNAME: Cannot restart postmaster. postgres is running (pid: $PID)" echo "$CMDNAME: Cannot restart postmaster. postgres is running (pid: $PID)" 1>&2
echo "Please terminate postgres and try again" echo "Please terminate postgres and try again." 1>&2
exit 1 exit 1
fi fi
kill $sig $PID kill $sig $PID
# wait for postmaster to shut down # wait for postmaster to shut down
if [ "$wait" = 1 -o $op = "restart" ];then if [ "$wait" = yes -o "$op" = restart ];then
cnt=0 cnt=0
$ECHO_N "Waiting for postmaster to shut down.."$ECHO_C $silence_echo $ECHO_N "waiting for postmaster to shut down..."$ECHO_C
while : while :
do do
if [ -f $PIDFILE ];then if [ -f $PIDFILE ];then
$ECHO_N "."$ECHO_C $silence_echo $ECHO_N "."$ECHO_C
cnt=`expr $cnt + 1` cnt=`expr $cnt + 1`
if [ $cnt -gt 60 ];then if [ $cnt -gt $wait_seconds ];then
echo "failed." $silence_echo echo " failed"
echo "postmaster does not shut down." echo "$CMDNAME: postmaster does not shut down" 1>&2
exit 1 exit 1
fi fi
else else
...@@ -235,76 +277,102 @@ if [ $op = "stop" -o $op = "restart" ];then ...@@ -235,76 +277,102 @@ if [ $op = "stop" -o $op = "restart" ];then
fi fi
sleep 1 sleep 1
done done
echo "done." $silence_echo echo "done"
fi fi
$silence_echo echo "postmaster successfully shut down"
echo "postmaster successfully shut down." else # ! -f $PIDFILE
echo "$CMDNAME: cannot find $PIDFILE" 1>&2
else echo "Is postmaster running?" 1>&2
echo "$CMDNAME: cannot find $PIDFILE"
echo "Is postmaster running?"
if [ $op = "restart" ];then if [ $op = "restart" ];then
echo "starting postmaster anyway..." echo "starting postmaster anyway" 1>&2
else else
exit 1 exit 1
fi fi
fi fi
fi fi # stop or restart
if [ $op = "start" -o $op = "restart" ];then if [ $op = "start" -o $op = "restart" ];then
if [ -f $PIDFILE ];then if [ -f $PIDFILE ];then
echo "$CMDNAME: It seems another postmaster is running. Trying to start postmaster anyway." echo "$CMDNAME: It seems another postmaster is running. Trying to start postmaster anyway." 1>&2
pid=`head -1 $PIDFILE` pid=`sed -n 1p $PIDFILE`
fi
unset logopt
if [ -n "$logfile" ]; then
logopt='</dev/null >>$logfile 2>&1'
else
# when starting without log file, redirect stderr to stdout, so
# pg_ctl can be invoked with >$logfile and still have pg_ctl's
# stderr on the terminal.
logopt='</dev/null 2>&1'
fi fi
# no -o given # no -o given
if [ -z "$POSTOPTS" ];then if [ -z "$POSTOPTS" ];then
if [ $op = "start" ];then if [ $op = "start" ];then
# if we are in start mode, then look for postmaster.opts.default # if we are in start mode, then look for postmaster.opts.default
if [ -f $DEFPOSTOPTS ];then if [ -f $DEFPOSTOPTS ]; then
$po_path -D $PGDATA `cat $DEFPOSTOPTS` & POSTOPTS=`cat $DEFPOSTOPTS`
else
$po_path -D $PGDATA &
fi fi
POSTOPTS="-D $PGDATA $POSTOPTS"
else else
# if we are in restart mode, then look postmaster.opts # if we are in restart mode, then look for postmaster.opts
`cat $POSTOPTSFILE` & set X `cat $POSTOPTSFILE`
shift
po_path=$1
shift
POSTOPTS=$@
fi fi
else else # -o given
# -o given POSTOPTS="-D $PGDATA $POSTOPTS"
$po_path -D $PGDATA $POSTOPTS &
fi fi
eval '$po_path' '$POSTOPTS' $logopt '&'
if [ -f $PIDFILE ];then if [ -f $PIDFILE ];then
if [ "`head -1 $PIDFILE`" = "$pid" ];then if [ "`sed -n 1p $PIDFILE`" = "$pid" ];then
echo "$CMDNAME: Cannot start postmaster. Is another postmaster is running?" echo "$CMDNAME: cannot start postmaster" 1>&2
echo "Examine the log output." 1>&2
exit 1 exit 1
fi fi
fi fi
# wait for postmaster to start up # wait for postmaster to start
if [ "$wait" = 1 ];then if [ "$wait" = yes ];then
cnt=0 cnt=0
$ECHO_N "Waiting for postmaster to start up.."$ECHO_C $silence_echo $ECHO_N "waiting for postmaster to start..."$ECHO_C
while : while :
do do
# FIXME: This is horribly misconceived.
# 1) If password authentication is set up, the connection will fail.
# 2) If a virtual host is set up, the connection may fail.
# 3) If network traffic filters are set up tight enough, the connection
# may fail.
# 4) When no Unix domain sockets are available, the connection will
# fail. (Using TCP/IP by default ain't better.)
# 5) When a different port is configured, the connection will fail
# or go to the wrong server.
# 6) If the dynamic loader is not set up correctly (for this user/at
# this time), psql will fail (to find libpq).
# 7) If psql is misconfigured, this may fail.
if "$PGPATH/psql" -l >/dev/null 2>&1 if "$PGPATH/psql" -l >/dev/null 2>&1
then then
break; break;
else else
$ECHO_N "."$ECHO_C $silence_echo $ECHO_N "."$ECHO_C
cnt=`expr $cnt + 1` cnt=`expr $cnt + 1`
if [ $cnt -gt 60 ];then if [ $cnt -gt $wait_seconds ];then
echo "$CMDNAME: postmaster does not start up" $silence_echo echo "failed"
echo "$CMDNAME: postmaster does not start" 1>&2
exit 1 exit 1
fi fi
sleep 1 sleep 1
fi fi
done done
echo "done" $silence_echo echo "done"
fi fi
$silence_echo echo "postmaster successfully started"
echo "postmaster successfully started up" fi # start or restart
fi
exit 0 exit 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment