Thread: Problem with stopping postmaster with pg_ctl

Problem with stopping postmaster with pg_ctl

From
"John Pagakis"
Date:
I'm trying to get Postgres working under Cygwin.  The good news is, it
mostly is.  The bad news is, I can't shut it down with pg_ctl.

I loaded the full Cygwin installation on my Win2K Pro machine, and later on
my XP Pro box.  Both behave the same way.

When I run pg_ctl stop, it motors for the duration of the wait period and
then says the postmaster does not shut down.

Upon further review ......

It looks like pg_ctl looks through the process list for the Postgres process
and then tries to kill it.  You can specify the shutdown as smart (which
translates to kill -TERM), fast (kill -INT) or immediate (kill -QUIT).

When the script hits the kill, I get Usage info on the screen!!  So, the
interpreter is not seeing this as a valid command line string for kill.  I
have echoed the command being generated out and it looks fine.  I can take
that same command and execute it: it shuts Postgres down.

Why would the interpreter rejecting the command line for kill when it
appears to be well formed?

Any help would be greatly appreciated.

__________________________________________________________________
John Pagakis
Email: john@pagakis.com

"Ideas are more powerful than guns.  We would
 not let our enemies have guns, why should we
 let them have ideas?"
        -- Josef Stalin

This signature generated by
     ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software, Inc.
                                              www.spazmodicfrog.com


Re: Problem with stopping postmaster with pg_ctl

From
Igor Pechtchanski
Date:
John,

FWIW, you forgot SIGHUP (="-s 1").
    Igor

On Wed, 17 Sep 2003, John Pagakis wrote:

> That did the trick Igor!! Thanks so much!!
>
> For any with the same problem, here's the revised script:
>
> #! /bin/sh
> #-------------------------------------------------------------------------
> #
> # pg_ctl.sh--
> #    Start/Stop/Restart/HUP/Report status of postmaster
> #
> # Copyright (c) 2001  PostgreSQL Global Development Group
> #
> #
> # IDENTIFICATION
> #    $Header: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.sh,v 1.30
> 2002/10/18 22:05:35 petere Exp $
> #
> #-------------------------------------------------------------------------
>
> CMDNAME=`basename $0`
>
> help="\
> $CMDNAME is a utility to start, stop, restart, reload configuration files,
> or report the status of a PostgreSQL server.
>
> Usage:
>   $CMDNAME start   [-w] [-D DATADIR] [-s] [-l FILENAME] [-o \"OPTIONS\"]
>   $CMDNAME stop    [-W] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
>   $CMDNAME restart [-w] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o
> \"OPTIONS\"]
>   $CMDNAME reload  [-D DATADIR] [-s]
>   $CMDNAME status  [-D DATADIR]
>
> Common options:
>   -D DATADIR   location of the database storage area
>   -s           only print errors, no informational messages
>   -w           wait until operation completes
>   -W           do not wait until operation completes
>   --help       show this help, then exit
>   --version    output version information, then exit
> (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
>                           (PostgreSQL server executable)
>   -p PATH-TO-POSTMASTER   normally not necessary
>
> Options for stop or restart:
>   -m SHUTDOWN-MODE   may be 'smart', 'fast', or 'immediate'
>
> Shutdown modes are:
>   smart       quit after all clients have disconnected
>   fast        quit directly, with proper shutdown
>   immediate   quit without complete shutdown; will lead to recovery on
> restart
>
> Report bugs to <pgsql-bugs@postgresql.org>."
>
> advice="\
> Try '$CMDNAME --help' for more information."
>
>
> # Placed here during build
> bindir='/usr/bin'
> VERSION='7.3.4'
>
> # protect the log file
> umask 077
>
> # Check for echo -n vs echo \c
>
> if echo '\c' | grep -s c >/dev/null 2>&1
> then
>     ECHO_N="echo -n"
>     ECHO_C=""
> else
>     ECHO_N="echo"
>     ECHO_C='\c'
> fi
>
> #
> # Find out where we're located
> #
> if echo "$0" | grep '/' > /dev/null 2>&1
> then
>         # explicit dir name given
>         self_path=`echo "$0" | sed 's,/[^/]*$,,'`       # (dirname command
> is not portable)
> else
>         # look for it in PATH ('which' command is not portable)
>         for dir in `echo "$PATH" | sed 's/:/ /g'`
>         do
>                 # empty entry in path means current dir
>                 [ -z "$dir" ] && dir='.'
>                 if [ -f "$dir/$CMDNAME" ]
>                 then
>                         self_path="$dir"
>                         break
>                 fi
>         done
> fi
>
> # Check if needed programs actually exist in path
> if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ]; then
>     PGPATH="$self_path"
> elif [ -x "$bindir/postmaster" ] && [ -x "$bindir/psql" ]; then
>     PGPATH="$bindir"
> else
>     echo "The programs 'postmaster' and 'psql' are needed by $CMDNAME but"
> 1>&2
>     echo "were not found in the directory '$bindir'." 1>&2
>     echo "Check your installation." 1>&2
>     exit 1
> fi
>
> po_path="$PGPATH/postmaster"
>
> wait=
> wait_seconds=60
> logfile=
> silence_echo=
> shutdown_mode=smart
>
> while [ "$#" -gt 0 ]
> do
>     case "$1" in
>         -h|--help|-\?)
>             echo "$help"
>             exit 0
>             ;;
>         -V|--version)
>             echo "pg_ctl (PostgreSQL) $VERSION"
>             exit 0
>             ;;
>         -D)
>             shift
>             # pass environment into new postmaster
>             PGDATA="$1"
>             export PGDATA
>             ;;
>         -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)
>             shift
>             po_path="$1"
>             ;;
>         -s)
>             silence_echo=:
>             ;;
>         -w)
>             wait=yes
>             ;;
>         -W)
>             wait=no
>             ;;
>         -*)
>             echo "$CMDNAME: invalid option: $1" 1>&2
>             echo "$advice" 1>&2
>             exit 1
>             ;;
>         start)
>             op="start"
>             ;;
>         stop)
>             op="stop"
>             ;;
>         restart)
>             op="restart"
>             ;;
>         reload)
>             op="reload"
>             ;;
>         status)
>             op="status"
>             ;;
>         *)
>             echo "$CMDNAME: invalid operation mode: $1" 1>&2
>             echo "$advice" 1>&2
>             exit 1
>             ;;
>     esac
>     shift
> done
>
> if [ x"$op" = x"" ];then
>     echo "$CMDNAME: no operation mode specified" 1>&2
>     echo "$advice" 1>&2
>     exit 1
> fi
>
> if [ -z "$PGDATA" ];then
>     echo "$CMDNAME: no database directory or environment variable \$PGDATA
> is specified" 1>&2
>     echo "$advice" 1>&2
>     exit 1
> 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"
>         sig="-s 15"
>         ;;
>     f|fast)
>         sig="-INT"
>         sig="-s 2"
>         ;;
>     i|immediate)
>         sig="-QUIT"
>         sig="-s 3"
>         ;;
>     *)
>         echo "$CMDNAME: invalid shutdown mode: $1" 1>&2
>         echo "$advice" 1>&2
>         exit 1
>         ;;
> esac
>
> if [ "$op" = "reload" ];then
>         sig="-HUP"
>         wait=no
> fi
>
> DEFPOSTOPTS=$PGDATA/postmaster.opts.default
> POSTOPTSFILE=$PGDATA/postmaster.opts
> PIDFILE=$PGDATA/postmaster.pid
>
> if [ "$op" = "status" ];then
>     if [ -f "$PIDFILE" ];then
>         PID=`sed -n 1p $PIDFILE`
>         if [ "$PID" -lt 0 ];then
>             PID=`expr 0 - $PID`
>             echo "$CMDNAME: postgres is running (pid: $PID)"
>         else
>             echo "$CMDNAME: postmaster is running (pid: $PID)"
>             echo "Command line was:"
>             cat "$POSTOPTSFILE"
>         fi
>         exit 0
>     else
>         echo "$CMDNAME: postmaster or postgres is not running"
>         exit 1
>     fi
> fi
>
> if [ "$op" = "stop" -o "$op" = "restart" -o "$op" = "reload" ];then
>     if [ -f "$PIDFILE" ];then
>         PID=`sed -n 1p $PIDFILE`
>         if [ "$PID" -lt 0 ];then
>             PID=`expr 0 - $PID`
>             echo "$CMDNAME: Cannot restart postmaster.  postgres is running (pid:
> $PID)" 1>&2
>             echo "Please terminate postgres and try again." 1>&2
>             exit 1
>         fi
>
>         kill "$sig" $PID
>
>         # wait for postmaster to shut down
>         if [ "$wait" = yes -o "$op" = restart ];then
>             cnt=0
>             $silence_echo $ECHO_N "waiting for postmaster to shut down..."$ECHO_C
>
>             while :
>             do
>                 if [ -f "$PIDFILE" ];then
>                     $silence_echo $ECHO_N "."$ECHO_C
>                     cnt=`expr $cnt + 1`
>                     if [ "$cnt" -gt "$wait_seconds" ];then
>                         $silence_echo echo " failed"
>                         echo "$CMDNAME: postmaster does not shut down" 1>&2
>                         exit 1
>                     fi
>                 else
>                     break
>                 fi
>                 sleep 1
>             done
>             $silence_echo echo "done"
>         fi
>
>         if [ "$op" = "reload" ];then
>             $silence_echo echo "postmaster successfully signaled"
>         else
>             $silence_echo echo "postmaster successfully shut down"
>         fi
>
>     else # ! -f $PIDFILE
>         echo "$CMDNAME: cannot find $PIDFILE" 1>&2
>         echo "Is postmaster running?" 1>&2
>         if [ "$op" = "restart" ];then
>             echo "starting postmaster anyway" 1>&2
>         else
>             exit 1
>         fi
>     fi
> fi # stop, restart, reload
>
> if [ "$op" = "start" -o "$op" = "restart" ];then
>     oldpid=""
>     if [ -f "$PIDFILE" ];then
>         echo "$CMDNAME: Another postmaster may be running.  Trying to start
> postmaster anyway." 1>&2
>         oldpid=`sed -n 1p $PIDFILE`
>     fi
>
>     # no -o given
>     if [ -z "$POSTOPTS" ];then
>         if [ "$op" = "start" ];then
>             # if we are in start mode, then look for postmaster.opts.default
>             if [ -f "$DEFPOSTOPTS" ]; then
>                 eval set X "`cat $DEFPOSTOPTS`"; shift
>             fi
>         else
>             # if we are in restart mode, then look for postmaster.opts
>             eval set X "`cat $POSTOPTSFILE`"; shift
>             po_path="$1"
>             shift
>         fi
>     else # -o given
>         eval set X "$POSTOPTS"; shift
>     fi
>
>     if [ -n "$logfile" ]; then
>         "$po_path" ${1+"$@"} </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.
>         "$po_path" ${1+"$@"} </dev/null 2>&1 &
>     fi
>
>     # if had an old lockfile, check to see if we were able to start
>     if [ -n "$oldpid" ];then
>         sleep 1
>         if [ -f "$PIDFILE" ];then
>             if [ "`sed -n 1p $PIDFILE`" = "$oldpid" ];then
>                 echo "$CMDNAME: cannot start postmaster" 1>&2
>                 echo "Examine the log output." 1>&2
>                 exit 1
>             fi
>         fi
>     fi
>
>     # wait for postmaster to start
>     if [ "$wait" = yes ];then
>         cnt=0
>         $silence_echo $ECHO_N "waiting for postmaster to start..."$ECHO_C
>         while :
>         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
>             then
>                 break;
>             else
>                 $silence_echo $ECHO_N "."$ECHO_C
>                 cnt=`expr $cnt + 1`
>                 if [ "$cnt" -gt "$wait_seconds" ];then
>                     $silence_echo echo "failed"
>                     echo "$CMDNAME: postmaster does not start" 1>&2
>                     exit 1
>                 fi
>                 sleep 1
>             fi
>         done
>         $silence_echo echo "done"
>     fi
>     $silence_echo echo "postmaster successfully started"
> fi # start or restart
>
> exit 0
>
> __________________________________________________________________
> John Pagakis
> Email: john@pagakis.com
>
> Cheese -- Milk's leap towards immortality.
>        -- Clifton Fadiman
>
> This signature generated by
>      ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software, Inc.
>                                               www.spazmodicfrog.com
>
>
> -----Original Message-----
> From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
> Sent: Wednesday, September 17, 2003 8:51 AM
> To: John Pagakis
> Cc: cygwin@cygwin.com
> Subject: RE: Problem with stopping postmaster with pg_ctl
>
>
> John,
>
> This turned out to be a problem with /bin/kill.exe in Cygwin 1.5.4, which
> should be fixed in the next release
> (<http://cygwin.com/ml/cygwin/2003-09/msg01101.html>).  If you're
> adventurous, try the snapshot.  Otherwise, a couple of quick workarounds
> until /bin/kill is fixed are to use different syntax or use bash's builtin
> kill.  For the former, change "kill -15 $PID" to "kill -s 15 $PID".  For
> the latter, either change the #! line in pg_ctl to "#!/bin/bash" instead
> of "#!/bin/sh", or force bash's kill by using 'bash -c "kill -15 $PID"'
> instead of "kill -15 $PID".
>
> Both of these will become unnecessary in the next Cygwin release, but
> won't hurt, and should keep you running until then.  Hope this helps,
>         Igor
>
> On Wed, 17 Sep 2003, John Pagakis wrote:
>
> > Oh, I meant /bin/kill not /etc/kill on that last post .....
> >
> > The way Postgres controls things is, when it starts up, it stores the PID
> it
> > started under in a file called postgresql.pid in the data directory.  When
> > you use "pg_ctl stop", it reads the pid file and the issues a kill -15 on
> > that pid.  It then sits and waits for postgresql.pid to disappear.  After
> > one minute, if the file is still there, the script gives up and announces
> > that the postmaster will not stop.
> >
> > If you try this, you'll notice that when you get to the kill in the
> script,
> > you'll get the Usage info on screen.  There is nothing wrong with how
> pg_ctl
> > is formatting the kill.  If you echo out that command and execute it from
> > the command line, it works just fine.
> >
> > I believe the problem is Cygwin's implementation of kill.  From the
> command
> > line if you say kill -sig pid, it works.  If you say /bin/kill -sig pid it
> > gives you Usage.  There is something about when you give the fully
> qualified
> > path that it finds offensive.  The script does not give the fully
> qualified
> > path, but based on the behavior, I'm guessing the interpreter resolves to
> > the fully qualified path before executing.
> >
> > Anyway, BEFORE you exit, do this:
> >
> > 1) ps
> > This gives you a list of active processes.
> >
> > 2) Find the pid for postgres who's ppid is 1.
> >
> > 3) kill -15 that pid.
> >
> > 4) Wait for the message that the database is shut down.
> >
> > Now you can exit.
> >
> > __________________________________________________________________
> > John Pagakis
> > Email: john@pagakis.com
> >
> > "With all your science can you tell how it is, and whence it is, that
> >  light comes into the soul?"
> >         -- Henry David Thoreau
> >
> > This signature generated by
> >      ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software,
> Inc.
> >                                               www.spazmodicfrog.com
> >
> >
> > -----Original Message-----
> > From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
> > Sent: Tuesday, September 16, 2003 1:47 PM
> > To: John Pagakis
> > Cc: pgsql-cygwin@postgresql.org; cygwin@cygwin.com
> > Subject: Re: Problem with stopping postmaster with pg_ctl
> >
> >
> > On Tue, 16 Sep 2003, John Pagakis wrote:
> >
> > > I'm trying to get Postgres working under Cygwin.  The good news is, it
> > > mostly is.  The bad news is, I can't shut it down with pg_ctl.
> > >
> > > I loaded the full Cygwin installation on my Win2K Pro machine, and later
> > on
> > > my XP Pro box.  Both behave the same way.
> > >
> > > When I run pg_ctl stop, it motors for the duration of the wait period
> and
> > > then says the postmaster does not shut down.
> > >
> > > Upon further review ......
> > >
> > > It looks like pg_ctl looks through the process list for the Postgres
> > process
> > > and then tries to kill it.  You can specify the shutdown as smart (which
> > > translates to kill -TERM), fast (kill -INT) or immediate (kill -QUIT).
> > >
> > > When the script hits the kill, I get Usage info on the screen!!  So, the
> > > interpreter is not seeing this as a valid command line string for kill.
> I
> > > have echoed the command being generated out and it looks fine.  I can
> take
> > > that same command and execute it: it shuts Postgres down.
> > >
> > > Why would the interpreter rejecting the command line for kill when it
> > > appears to be well formed?
> > >
> > > Any help would be greatly appreciated.
> >
> > You probably have another "kill" in the path before "/bin/kill".  Try
> > "bash -c 'exec -l sh'", and from there "which kill".
> >
> > Had you attached the output of "cygcheck -svr", as requested in the
> > problem reporting guidelines at <http://cygwin.com/problems.html>, it
> > would have provided some information for a more intelligent guess.
> >         Igor
>
> --
>                                 http://cs.nyu.edu/~pechtcha/
>       |\      _,,,---,,_                pechtcha@cs.nyu.edu
> ZZZzz /,`.-'`'    -.  ;-;;,_            igor@watson.ibm.com
>      |,4-  ) )-,_. ,\ (  `'-'           Igor Pechtchanski, Ph.D.
>     '---''(_/--'  `-'\_) fL     a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!
>
> "I have since come to realize that being between your mentor and his route
> to the bathroom is a major career booster."  -- Patrick Naughton
>
>
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Problem reports:       http://cygwin.com/problems.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
>

--
                http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_        pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_        igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'        Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL    a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"I have since come to realize that being between your mentor and his route
to the bathroom is a major career booster."  -- Patrick Naughton

Re: Problem with stopping postmaster with pg_ctl

From
"John Pagakis"
Date:
That did the trick Igor!! Thanks so much!!

For any with the same problem, here's the revised script:

#! /bin/sh
#-------------------------------------------------------------------------
#
# pg_ctl.sh--
#    Start/Stop/Restart/HUP/Report status of postmaster
#
# Copyright (c) 2001  PostgreSQL Global Development Group
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.sh,v 1.30
2002/10/18 22:05:35 petere Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`

help="\
$CMDNAME is a utility to start, stop, restart, reload configuration files,
or report the status of a PostgreSQL server.

Usage:
  $CMDNAME start   [-w] [-D DATADIR] [-s] [-l FILENAME] [-o \"OPTIONS\"]
  $CMDNAME stop    [-W] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
  $CMDNAME restart [-w] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o
\"OPTIONS\"]
  $CMDNAME reload  [-D DATADIR] [-s]
  $CMDNAME status  [-D DATADIR]

Common options:
  -D DATADIR   location of the database storage area
  -s           only print errors, no informational messages
  -w           wait until operation completes
  -W           do not wait until operation completes
  --help       show this help, then exit
  --version    output version information, then exit
(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
                          (PostgreSQL server executable)
  -p PATH-TO-POSTMASTER   normally not necessary

Options for stop or restart:
  -m SHUTDOWN-MODE   may be 'smart', 'fast', or 'immediate'

Shutdown modes are:
  smart       quit after all clients have disconnected
  fast        quit directly, with proper shutdown
  immediate   quit without complete shutdown; will lead to recovery on
restart

Report bugs to <pgsql-bugs@postgresql.org>."

advice="\
Try '$CMDNAME --help' for more information."


# Placed here during build
bindir='/usr/bin'
VERSION='7.3.4'

# protect the log file
umask 077

# Check for echo -n vs echo \c

if echo '\c' | grep -s c >/dev/null 2>&1
then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi

#
# Find out where we're located
#
if echo "$0" | grep '/' > /dev/null 2>&1
then
        # explicit dir name given
        self_path=`echo "$0" | sed 's,/[^/]*$,,'`       # (dirname command
is not portable)
else
        # look for it in PATH ('which' command is not portable)
        for dir in `echo "$PATH" | sed 's/:/ /g'`
    do
                # empty entry in path means current dir
                [ -z "$dir" ] && dir='.'
                if [ -f "$dir/$CMDNAME" ]
        then
                        self_path="$dir"
                        break
                fi
        done
fi

# Check if needed programs actually exist in path
if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ]; then
    PGPATH="$self_path"
elif [ -x "$bindir/postmaster" ] && [ -x "$bindir/psql" ]; then
    PGPATH="$bindir"
else
    echo "The programs 'postmaster' and 'psql' are needed by $CMDNAME but"
1>&2
    echo "were not found in the directory '$bindir'." 1>&2
    echo "Check your installation." 1>&2
    exit 1
fi

po_path="$PGPATH/postmaster"

wait=
wait_seconds=60
logfile=
silence_echo=
shutdown_mode=smart

while [ "$#" -gt 0 ]
do
    case "$1" in
    -h|--help|-\?)
        echo "$help"
        exit 0
        ;;
        -V|--version)
        echo "pg_ctl (PostgreSQL) $VERSION"
        exit 0
        ;;
    -D)
        shift
        # pass environment into new postmaster
        PGDATA="$1"
        export PGDATA
        ;;
    -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)
        shift
        po_path="$1"
        ;;
    -s)
        silence_echo=:
        ;;
    -w)
        wait=yes
        ;;
    -W)
        wait=no
        ;;
    -*)
        echo "$CMDNAME: invalid option: $1" 1>&2
        echo "$advice" 1>&2
        exit 1
        ;;
    start)
        op="start"
        ;;
    stop)
        op="stop"
        ;;
    restart)
        op="restart"
        ;;
    reload)
        op="reload"
        ;;
    status)
        op="status"
        ;;
    *)
        echo "$CMDNAME: invalid operation mode: $1" 1>&2
        echo "$advice" 1>&2
        exit 1
        ;;
    esac
    shift
done

if [ x"$op" = x"" ];then
    echo "$CMDNAME: no operation mode specified" 1>&2
    echo "$advice" 1>&2
    exit 1
fi

if [ -z "$PGDATA" ];then
    echo "$CMDNAME: no database directory or environment variable \$PGDATA
is specified" 1>&2
    echo "$advice" 1>&2
    exit 1
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"
        sig="-s 15"
    ;;
    f|fast)
    sig="-INT"
        sig="-s 2"
    ;;
    i|immediate)
    sig="-QUIT"
        sig="-s 3"
    ;;
    *)
    echo "$CMDNAME: invalid shutdown mode: $1" 1>&2
    echo "$advice" 1>&2
    exit 1
    ;;
esac

if [ "$op" = "reload" ];then
    sig="-HUP"
    wait=no
fi

DEFPOSTOPTS=$PGDATA/postmaster.opts.default
POSTOPTSFILE=$PGDATA/postmaster.opts
PIDFILE=$PGDATA/postmaster.pid

if [ "$op" = "status" ];then
    if [ -f "$PIDFILE" ];then
    PID=`sed -n 1p $PIDFILE`
    if [ "$PID" -lt 0 ];then
        PID=`expr 0 - $PID`
        echo "$CMDNAME: postgres is running (pid: $PID)"
    else
        echo "$CMDNAME: postmaster is running (pid: $PID)"
        echo "Command line was:"
        cat "$POSTOPTSFILE"
    fi
    exit 0
    else
    echo "$CMDNAME: postmaster or postgres is not running"
    exit 1
    fi
fi

if [ "$op" = "stop" -o "$op" = "restart" -o "$op" = "reload" ];then
    if [ -f "$PIDFILE" ];then
    PID=`sed -n 1p $PIDFILE`
    if [ "$PID" -lt 0 ];then
        PID=`expr 0 - $PID`
        echo "$CMDNAME: Cannot restart postmaster.  postgres is running (pid:
$PID)" 1>&2
        echo "Please terminate postgres and try again." 1>&2
        exit 1
    fi

    kill "$sig" $PID

    # wait for postmaster to shut down
    if [ "$wait" = yes -o "$op" = restart ];then
        cnt=0
        $silence_echo $ECHO_N "waiting for postmaster to shut down..."$ECHO_C

        while :
        do
        if [ -f "$PIDFILE" ];then
            $silence_echo $ECHO_N "."$ECHO_C
            cnt=`expr $cnt + 1`
            if [ "$cnt" -gt "$wait_seconds" ];then
            $silence_echo echo " failed"
            echo "$CMDNAME: postmaster does not shut down" 1>&2
            exit 1
            fi
        else
            break
        fi
        sleep 1
        done
        $silence_echo echo "done"
    fi

    if [ "$op" = "reload" ];then
        $silence_echo echo "postmaster successfully signaled"
    else
        $silence_echo echo "postmaster successfully shut down"
    fi

    else # ! -f $PIDFILE
    echo "$CMDNAME: cannot find $PIDFILE" 1>&2
    echo "Is postmaster running?" 1>&2
    if [ "$op" = "restart" ];then
        echo "starting postmaster anyway" 1>&2
    else
        exit 1
    fi
    fi
fi # stop, restart, reload

if [ "$op" = "start" -o "$op" = "restart" ];then
    oldpid=""
    if [ -f "$PIDFILE" ];then
    echo "$CMDNAME: Another postmaster may be running.  Trying to start
postmaster anyway." 1>&2
    oldpid=`sed -n 1p $PIDFILE`
    fi

    # no -o given
    if [ -z "$POSTOPTS" ];then
    if [ "$op" = "start" ];then
        # if we are in start mode, then look for postmaster.opts.default
        if [ -f "$DEFPOSTOPTS" ]; then
        eval set X "`cat $DEFPOSTOPTS`"; shift
        fi
    else
        # if we are in restart mode, then look for postmaster.opts
        eval set X "`cat $POSTOPTSFILE`"; shift
            po_path="$1"
            shift
    fi
    else # -o given
        eval set X "$POSTOPTS"; shift
    fi

    if [ -n "$logfile" ]; then
        "$po_path" ${1+"$@"} </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.
        "$po_path" ${1+"$@"} </dev/null 2>&1 &
    fi

    # if had an old lockfile, check to see if we were able to start
    if [ -n "$oldpid" ];then
    sleep 1
    if [ -f "$PIDFILE" ];then
        if [ "`sed -n 1p $PIDFILE`" = "$oldpid" ];then
        echo "$CMDNAME: cannot start postmaster" 1>&2
        echo "Examine the log output." 1>&2
        exit 1
        fi
        fi
    fi

    # wait for postmaster to start
    if [ "$wait" = yes ];then
    cnt=0
    $silence_echo $ECHO_N "waiting for postmaster to start..."$ECHO_C
    while :
    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
        then
        break;
        else
        $silence_echo $ECHO_N "."$ECHO_C
        cnt=`expr $cnt + 1`
        if [ "$cnt" -gt "$wait_seconds" ];then
            $silence_echo echo "failed"
            echo "$CMDNAME: postmaster does not start" 1>&2
            exit 1
        fi
        sleep 1
        fi
    done
    $silence_echo echo "done"
    fi
    $silence_echo echo "postmaster successfully started"
fi # start or restart

exit 0

__________________________________________________________________
John Pagakis
Email: john@pagakis.com

Cheese -- Milk's leap towards immortality.
       -- Clifton Fadiman

This signature generated by
     ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software, Inc.
                                              www.spazmodicfrog.com


-----Original Message-----
From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
Sent: Wednesday, September 17, 2003 8:51 AM
To: John Pagakis
Cc: cygwin@cygwin.com
Subject: RE: Problem with stopping postmaster with pg_ctl


John,

This turned out to be a problem with /bin/kill.exe in Cygwin 1.5.4, which
should be fixed in the next release
(<http://cygwin.com/ml/cygwin/2003-09/msg01101.html>).  If you're
adventurous, try the snapshot.  Otherwise, a couple of quick workarounds
until /bin/kill is fixed are to use different syntax or use bash's builtin
kill.  For the former, change "kill -15 $PID" to "kill -s 15 $PID".  For
the latter, either change the #! line in pg_ctl to "#!/bin/bash" instead
of "#!/bin/sh", or force bash's kill by using 'bash -c "kill -15 $PID"'
instead of "kill -15 $PID".

Both of these will become unnecessary in the next Cygwin release, but
won't hurt, and should keep you running until then.  Hope this helps,
    Igor

On Wed, 17 Sep 2003, John Pagakis wrote:

> Oh, I meant /bin/kill not /etc/kill on that last post .....
>
> The way Postgres controls things is, when it starts up, it stores the PID
it
> started under in a file called postgresql.pid in the data directory.  When
> you use "pg_ctl stop", it reads the pid file and the issues a kill -15 on
> that pid.  It then sits and waits for postgresql.pid to disappear.  After
> one minute, if the file is still there, the script gives up and announces
> that the postmaster will not stop.
>
> If you try this, you'll notice that when you get to the kill in the
script,
> you'll get the Usage info on screen.  There is nothing wrong with how
pg_ctl
> is formatting the kill.  If you echo out that command and execute it from
> the command line, it works just fine.
>
> I believe the problem is Cygwin's implementation of kill.  From the
command
> line if you say kill -sig pid, it works.  If you say /bin/kill -sig pid it
> gives you Usage.  There is something about when you give the fully
qualified
> path that it finds offensive.  The script does not give the fully
qualified
> path, but based on the behavior, I'm guessing the interpreter resolves to
> the fully qualified path before executing.
>
> Anyway, BEFORE you exit, do this:
>
> 1) ps
> This gives you a list of active processes.
>
> 2) Find the pid for postgres who's ppid is 1.
>
> 3) kill -15 that pid.
>
> 4) Wait for the message that the database is shut down.
>
> Now you can exit.
>
> __________________________________________________________________
> John Pagakis
> Email: john@pagakis.com
>
> "With all your science can you tell how it is, and whence it is, that
>  light comes into the soul?"
>         -- Henry David Thoreau
>
> This signature generated by
>      ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software,
Inc.
>                                               www.spazmodicfrog.com
>
>
> -----Original Message-----
> From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
> Sent: Tuesday, September 16, 2003 1:47 PM
> To: John Pagakis
> Cc: pgsql-cygwin@postgresql.org; cygwin@cygwin.com
> Subject: Re: Problem with stopping postmaster with pg_ctl
>
>
> On Tue, 16 Sep 2003, John Pagakis wrote:
>
> > I'm trying to get Postgres working under Cygwin.  The good news is, it
> > mostly is.  The bad news is, I can't shut it down with pg_ctl.
> >
> > I loaded the full Cygwin installation on my Win2K Pro machine, and later
> on
> > my XP Pro box.  Both behave the same way.
> >
> > When I run pg_ctl stop, it motors for the duration of the wait period
and
> > then says the postmaster does not shut down.
> >
> > Upon further review ......
> >
> > It looks like pg_ctl looks through the process list for the Postgres
> process
> > and then tries to kill it.  You can specify the shutdown as smart (which
> > translates to kill -TERM), fast (kill -INT) or immediate (kill -QUIT).
> >
> > When the script hits the kill, I get Usage info on the screen!!  So, the
> > interpreter is not seeing this as a valid command line string for kill.
I
> > have echoed the command being generated out and it looks fine.  I can
take
> > that same command and execute it: it shuts Postgres down.
> >
> > Why would the interpreter rejecting the command line for kill when it
> > appears to be well formed?
> >
> > Any help would be greatly appreciated.
>
> You probably have another "kill" in the path before "/bin/kill".  Try
> "bash -c 'exec -l sh'", and from there "which kill".
>
> Had you attached the output of "cygcheck -svr", as requested in the
> problem reporting guidelines at <http://cygwin.com/problems.html>, it
> would have provided some information for a more intelligent guess.
>         Igor

--
                http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_        pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_        igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'        Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL    a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"I have since come to realize that being between your mentor and his route
to the bathroom is a major career booster."  -- Patrick Naughton


Re: Problem with stopping postmaster with pg_ctl

From
"John Pagakis"
Date:
My bad!!  Thanks!!

Here's the new work-around script .......

#! /bin/sh
#-------------------------------------------------------------------------
#
# pg_ctl.sh--
#    Start/Stop/Restart/HUP/Report status of postmaster
#
# Copyright (c) 2001  PostgreSQL Global Development Group
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.sh,v 1.30
2002/10/18 22:05:35 petere Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`

help="\
$CMDNAME is a utility to start, stop, restart, reload configuration files,
or report the status of a PostgreSQL server.

Usage:
  $CMDNAME start   [-w] [-D DATADIR] [-s] [-l FILENAME] [-o \"OPTIONS\"]
  $CMDNAME stop    [-W] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
  $CMDNAME restart [-w] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o
\"OPTIONS\"]
  $CMDNAME reload  [-D DATADIR] [-s]
  $CMDNAME status  [-D DATADIR]

Common options:
  -D DATADIR   location of the database storage area
  -s           only print errors, no informational messages
  -w           wait until operation completes
  -W           do not wait until operation completes
  --help       show this help, then exit
  --version    output version information, then exit
(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
                          (PostgreSQL server executable)
  -p PATH-TO-POSTMASTER   normally not necessary

Options for stop or restart:
  -m SHUTDOWN-MODE   may be 'smart', 'fast', or 'immediate'

Shutdown modes are:
  smart       quit after all clients have disconnected
  fast        quit directly, with proper shutdown
  immediate   quit without complete shutdown; will lead to recovery on
restart

Report bugs to <pgsql-bugs@postgresql.org>."

advice="\
Try '$CMDNAME --help' for more information."


# Placed here during build
bindir='/usr/bin'
VERSION='7.3.4'

# protect the log file
umask 077

# Check for echo -n vs echo \c

if echo '\c' | grep -s c >/dev/null 2>&1
then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi

#
# Find out where we're located
#
if echo "$0" | grep '/' > /dev/null 2>&1
then
        # explicit dir name given
        self_path=`echo "$0" | sed 's,/[^/]*$,,'`       # (dirname command
is not portable)
else
        # look for it in PATH ('which' command is not portable)
        for dir in `echo "$PATH" | sed 's/:/ /g'`
    do
                # empty entry in path means current dir
                [ -z "$dir" ] && dir='.'
                if [ -f "$dir/$CMDNAME" ]
        then
                        self_path="$dir"
                        break
                fi
        done
fi

# Check if needed programs actually exist in path
if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ]; then
    PGPATH="$self_path"
elif [ -x "$bindir/postmaster" ] && [ -x "$bindir/psql" ]; then
    PGPATH="$bindir"
else
    echo "The programs 'postmaster' and 'psql' are needed by $CMDNAME but"
1>&2
    echo "were not found in the directory '$bindir'." 1>&2
    echo "Check your installation." 1>&2
    exit 1
fi

po_path="$PGPATH/postmaster"

wait=
wait_seconds=60
logfile=
silence_echo=
shutdown_mode=smart

while [ "$#" -gt 0 ]
do
    case "$1" in
    -h|--help|-\?)
        echo "$help"
        exit 0
        ;;
        -V|--version)
        echo "pg_ctl (PostgreSQL) $VERSION"
        exit 0
        ;;
    -D)
        shift
        # pass environment into new postmaster
        PGDATA="$1"
        export PGDATA
        ;;
    -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)
        shift
        po_path="$1"
        ;;
    -s)
        silence_echo=:
        ;;
    -w)
        wait=yes
        ;;
    -W)
        wait=no
        ;;
    -*)
        echo "$CMDNAME: invalid option: $1" 1>&2
        echo "$advice" 1>&2
        exit 1
        ;;
    start)
        op="start"
        ;;
    stop)
        op="stop"
        ;;
    restart)
        op="restart"
        ;;
    reload)
        op="reload"
        ;;
    status)
        op="status"
        ;;
    *)
        echo "$CMDNAME: invalid operation mode: $1" 1>&2
        echo "$advice" 1>&2
        exit 1
        ;;
    esac
    shift
done

if [ x"$op" = x"" ];then
    echo "$CMDNAME: no operation mode specified" 1>&2
    echo "$advice" 1>&2
    exit 1
fi

if [ -z "$PGDATA" ];then
    echo "$CMDNAME: no database directory or environment variable \$PGDATA
is specified" 1>&2
    echo "$advice" 1>&2
    exit 1
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"
        sig="-s 15"
    ;;
    f|fast)
    sig="-INT"
        sig="-s 2"
    ;;
    i|immediate)
    sig="-QUIT"
        sig="-s 3"
    ;;
    *)
    echo "$CMDNAME: invalid shutdown mode: $1" 1>&2
    echo "$advice" 1>&2
    exit 1
    ;;
esac

if [ "$op" = "reload" ];then
    sig="-HUP"
    sig="-s 1"
    wait=no
fi

DEFPOSTOPTS=$PGDATA/postmaster.opts.default
POSTOPTSFILE=$PGDATA/postmaster.opts
PIDFILE=$PGDATA/postmaster.pid

if [ "$op" = "status" ];then
    if [ -f "$PIDFILE" ];then
    PID=`sed -n 1p $PIDFILE`
    if [ "$PID" -lt 0 ];then
        PID=`expr 0 - $PID`
        echo "$CMDNAME: postgres is running (pid: $PID)"
    else
        echo "$CMDNAME: postmaster is running (pid: $PID)"
        echo "Command line was:"
        cat "$POSTOPTSFILE"
    fi
    exit 0
    else
    echo "$CMDNAME: postmaster or postgres is not running"
    exit 1
    fi
fi

if [ "$op" = "stop" -o "$op" = "restart" -o "$op" = "reload" ];then
    if [ -f "$PIDFILE" ];then
    PID=`sed -n 1p $PIDFILE`
    if [ "$PID" -lt 0 ];then
        PID=`expr 0 - $PID`
        echo "$CMDNAME: Cannot restart postmaster.  postgres is running (pid:
$PID)" 1>&2
        echo "Please terminate postgres and try again." 1>&2
        exit 1
    fi

    kill "$sig" $PID

    # wait for postmaster to shut down
    if [ "$wait" = yes -o "$op" = restart ];then
        cnt=0
        $silence_echo $ECHO_N "waiting for postmaster to shut down..."$ECHO_C

        while :
        do
        if [ -f "$PIDFILE" ];then
            $silence_echo $ECHO_N "."$ECHO_C
            cnt=`expr $cnt + 1`
            if [ "$cnt" -gt "$wait_seconds" ];then
            $silence_echo echo " failed"
            echo "$CMDNAME: postmaster does not shut down" 1>&2
            exit 1
            fi
        else
            break
        fi
        sleep 1
        done
        $silence_echo echo "done"
    fi

    if [ "$op" = "reload" ];then
        $silence_echo echo "postmaster successfully signaled"
    else
        $silence_echo echo "postmaster successfully shut down"
    fi

    else # ! -f $PIDFILE
    echo "$CMDNAME: cannot find $PIDFILE" 1>&2
    echo "Is postmaster running?" 1>&2
    if [ "$op" = "restart" ];then
        echo "starting postmaster anyway" 1>&2
    else
        exit 1
    fi
    fi
fi # stop, restart, reload

if [ "$op" = "start" -o "$op" = "restart" ];then
    oldpid=""
    if [ -f "$PIDFILE" ];then
    echo "$CMDNAME: Another postmaster may be running.  Trying to start
postmaster anyway." 1>&2
    oldpid=`sed -n 1p $PIDFILE`
    fi

    # no -o given
    if [ -z "$POSTOPTS" ];then
    if [ "$op" = "start" ];then
        # if we are in start mode, then look for postmaster.opts.default
        if [ -f "$DEFPOSTOPTS" ]; then
        eval set X "`cat $DEFPOSTOPTS`"; shift
        fi
    else
        # if we are in restart mode, then look for postmaster.opts
        eval set X "`cat $POSTOPTSFILE`"; shift
            po_path="$1"
            shift
    fi
    else # -o given
        eval set X "$POSTOPTS"; shift
    fi

    if [ -n "$logfile" ]; then
        "$po_path" ${1+"$@"} </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.
        "$po_path" ${1+"$@"} </dev/null 2>&1 &
    fi

    # if had an old lockfile, check to see if we were able to start
    if [ -n "$oldpid" ];then
    sleep 1
    if [ -f "$PIDFILE" ];then
        if [ "`sed -n 1p $PIDFILE`" = "$oldpid" ];then
        echo "$CMDNAME: cannot start postmaster" 1>&2
        echo "Examine the log output." 1>&2
        exit 1
        fi
        fi
    fi

    # wait for postmaster to start
    if [ "$wait" = yes ];then
    cnt=0
    $silence_echo $ECHO_N "waiting for postmaster to start..."$ECHO_C
    while :
    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
        then
        break;
        else
        $silence_echo $ECHO_N "."$ECHO_C
        cnt=`expr $cnt + 1`
        if [ "$cnt" -gt "$wait_seconds" ];then
            $silence_echo echo "failed"
            echo "$CMDNAME: postmaster does not start" 1>&2
            exit 1
        fi
        sleep 1
        fi
    done
    $silence_echo echo "done"
    fi
    $silence_echo echo "postmaster successfully started"
fi # start or restart

exit 0

__________________________________________________________________
John Pagakis
Email: john@pagakis.com

"What we play is life."
        -- Louis Armstrong

This signature generated by
     ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software, Inc.
                                              www.spazmodicfrog.com


-----Original Message-----
From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
Sent: Wednesday, September 17, 2003 10:05 AM
To: John Pagakis
Cc: cygwin@cygwin.com; pgsql-cygwin@postgresql.org
Subject: RE: Problem with stopping postmaster with pg_ctl


John,

FWIW, you forgot SIGHUP (="-s 1").
    Igor

On Wed, 17 Sep 2003, John Pagakis wrote:

> That did the trick Igor!! Thanks so much!!
>
> For any with the same problem, here's the revised script:
>
> #! /bin/sh
> #-------------------------------------------------------------------------
> #
> # pg_ctl.sh--
> #    Start/Stop/Restart/HUP/Report status of postmaster
> #
> # Copyright (c) 2001  PostgreSQL Global Development Group
> #
> #
> # IDENTIFICATION
> #    $Header: /cvsroot/pgsql-server/src/bin/pg_ctl/pg_ctl.sh,v 1.30
> 2002/10/18 22:05:35 petere Exp $
> #
> #-------------------------------------------------------------------------
>
> CMDNAME=`basename $0`
>
> help="\
> $CMDNAME is a utility to start, stop, restart, reload configuration files,
> or report the status of a PostgreSQL server.
>
> Usage:
>   $CMDNAME start   [-w] [-D DATADIR] [-s] [-l FILENAME] [-o \"OPTIONS\"]
>   $CMDNAME stop    [-W] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
>   $CMDNAME restart [-w] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o
> \"OPTIONS\"]
>   $CMDNAME reload  [-D DATADIR] [-s]
>   $CMDNAME status  [-D DATADIR]
>
> Common options:
>   -D DATADIR   location of the database storage area
>   -s           only print errors, no informational messages
>   -w           wait until operation completes
>   -W           do not wait until operation completes
>   --help       show this help, then exit
>   --version    output version information, then exit
> (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
>                           (PostgreSQL server executable)
>   -p PATH-TO-POSTMASTER   normally not necessary
>
> Options for stop or restart:
>   -m SHUTDOWN-MODE   may be 'smart', 'fast', or 'immediate'
>
> Shutdown modes are:
>   smart       quit after all clients have disconnected
>   fast        quit directly, with proper shutdown
>   immediate   quit without complete shutdown; will lead to recovery on
> restart
>
> Report bugs to <pgsql-bugs@postgresql.org>."
>
> advice="\
> Try '$CMDNAME --help' for more information."
>
>
> # Placed here during build
> bindir='/usr/bin'
> VERSION='7.3.4'
>
> # protect the log file
> umask 077
>
> # Check for echo -n vs echo \c
>
> if echo '\c' | grep -s c >/dev/null 2>&1
> then
>     ECHO_N="echo -n"
>     ECHO_C=""
> else
>     ECHO_N="echo"
>     ECHO_C='\c'
> fi
>
> #
> # Find out where we're located
> #
> if echo "$0" | grep '/' > /dev/null 2>&1
> then
>         # explicit dir name given
>         self_path=`echo "$0" | sed 's,/[^/]*$,,'`       # (dirname command
> is not portable)
> else
>         # look for it in PATH ('which' command is not portable)
>         for dir in `echo "$PATH" | sed 's/:/ /g'`
>         do
>                 # empty entry in path means current dir
>                 [ -z "$dir" ] && dir='.'
>                 if [ -f "$dir/$CMDNAME" ]
>                 then
>                         self_path="$dir"
>                         break
>                 fi
>         done
> fi
>
> # Check if needed programs actually exist in path
> if [ -x "$self_path/postmaster" ] && [ -x "$self_path/psql" ]; then
>     PGPATH="$self_path"
> elif [ -x "$bindir/postmaster" ] && [ -x "$bindir/psql" ]; then
>     PGPATH="$bindir"
> else
>     echo "The programs 'postmaster' and 'psql' are needed by $CMDNAME but"
> 1>&2
>     echo "were not found in the directory '$bindir'." 1>&2
>     echo "Check your installation." 1>&2
>     exit 1
> fi
>
> po_path="$PGPATH/postmaster"
>
> wait=
> wait_seconds=60
> logfile=
> silence_echo=
> shutdown_mode=smart
>
> while [ "$#" -gt 0 ]
> do
>     case "$1" in
>         -h|--help|-\?)
>             echo "$help"
>             exit 0
>             ;;
>         -V|--version)
>             echo "pg_ctl (PostgreSQL) $VERSION"
>             exit 0
>             ;;
>         -D)
>             shift
>             # pass environment into new postmaster
>             PGDATA="$1"
>             export PGDATA
>             ;;
>         -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)
>             shift
>             po_path="$1"
>             ;;
>         -s)
>             silence_echo=:
>             ;;
>         -w)
>             wait=yes
>             ;;
>         -W)
>             wait=no
>             ;;
>         -*)
>             echo "$CMDNAME: invalid option: $1" 1>&2
>             echo "$advice" 1>&2
>             exit 1
>             ;;
>         start)
>             op="start"
>             ;;
>         stop)
>             op="stop"
>             ;;
>         restart)
>             op="restart"
>             ;;
>         reload)
>             op="reload"
>             ;;
>         status)
>             op="status"
>             ;;
>         *)
>             echo "$CMDNAME: invalid operation mode: $1" 1>&2
>             echo "$advice" 1>&2
>             exit 1
>             ;;
>     esac
>     shift
> done
>
> if [ x"$op" = x"" ];then
>     echo "$CMDNAME: no operation mode specified" 1>&2
>     echo "$advice" 1>&2
>     exit 1
> fi
>
> if [ -z "$PGDATA" ];then
>     echo "$CMDNAME: no database directory or environment variable \$PGDATA
> is specified" 1>&2
>     echo "$advice" 1>&2
>     exit 1
> 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"
>         sig="-s 15"
>         ;;
>     f|fast)
>         sig="-INT"
>         sig="-s 2"
>         ;;
>     i|immediate)
>         sig="-QUIT"
>         sig="-s 3"
>         ;;
>     *)
>         echo "$CMDNAME: invalid shutdown mode: $1" 1>&2
>         echo "$advice" 1>&2
>         exit 1
>         ;;
> esac
>
> if [ "$op" = "reload" ];then
>         sig="-HUP"
>         wait=no
> fi
>
> DEFPOSTOPTS=$PGDATA/postmaster.opts.default
> POSTOPTSFILE=$PGDATA/postmaster.opts
> PIDFILE=$PGDATA/postmaster.pid
>
> if [ "$op" = "status" ];then
>     if [ -f "$PIDFILE" ];then
>         PID=`sed -n 1p $PIDFILE`
>         if [ "$PID" -lt 0 ];then
>             PID=`expr 0 - $PID`
>             echo "$CMDNAME: postgres is running (pid: $PID)"
>         else
>             echo "$CMDNAME: postmaster is running (pid: $PID)"
>             echo "Command line was:"
>             cat "$POSTOPTSFILE"
>         fi
>         exit 0
>     else
>         echo "$CMDNAME: postmaster or postgres is not running"
>         exit 1
>     fi
> fi
>
> if [ "$op" = "stop" -o "$op" = "restart" -o "$op" = "reload" ];then
>     if [ -f "$PIDFILE" ];then
>         PID=`sed -n 1p $PIDFILE`
>         if [ "$PID" -lt 0 ];then
>             PID=`expr 0 - $PID`
>             echo "$CMDNAME: Cannot restart postmaster.  postgres is
running (pid:
> $PID)" 1>&2
>             echo "Please terminate postgres and try again." 1>&2
>             exit 1
>         fi
>
>         kill "$sig" $PID
>
>         # wait for postmaster to shut down
>         if [ "$wait" = yes -o "$op" = restart ];then
>             cnt=0
>             $silence_echo $ECHO_N "waiting for postmaster to shut
down..."$ECHO_C
>
>             while :
>             do
>                 if [ -f "$PIDFILE" ];then
>                     $silence_echo $ECHO_N "."$ECHO_C
>                     cnt=`expr $cnt + 1`
>                     if [ "$cnt" -gt "$wait_seconds" ];then
>                         $silence_echo echo " failed"
>                         echo "$CMDNAME: postmaster does not shut down"
1>&2
>                         exit 1
>                     fi
>                 else
>                     break
>                 fi
>                 sleep 1
>             done
>             $silence_echo echo "done"
>         fi
>
>         if [ "$op" = "reload" ];then
>             $silence_echo echo "postmaster successfully signaled"
>         else
>             $silence_echo echo "postmaster successfully shut down"
>         fi
>
>     else # ! -f $PIDFILE
>         echo "$CMDNAME: cannot find $PIDFILE" 1>&2
>         echo "Is postmaster running?" 1>&2
>         if [ "$op" = "restart" ];then
>             echo "starting postmaster anyway" 1>&2
>         else
>             exit 1
>         fi
>     fi
> fi # stop, restart, reload
>
> if [ "$op" = "start" -o "$op" = "restart" ];then
>     oldpid=""
>     if [ -f "$PIDFILE" ];then
>         echo "$CMDNAME: Another postmaster may be running.  Trying to
start
> postmaster anyway." 1>&2
>         oldpid=`sed -n 1p $PIDFILE`
>     fi
>
>     # no -o given
>     if [ -z "$POSTOPTS" ];then
>         if [ "$op" = "start" ];then
>             # if we are in start mode, then look for
postmaster.opts.default
>             if [ -f "$DEFPOSTOPTS" ]; then
>                 eval set X "`cat $DEFPOSTOPTS`"; shift
>             fi
>         else
>             # if we are in restart mode, then look for postmaster.opts
>             eval set X "`cat $POSTOPTSFILE`"; shift
>             po_path="$1"
>             shift
>         fi
>     else # -o given
>         eval set X "$POSTOPTS"; shift
>     fi
>
>     if [ -n "$logfile" ]; then
>         "$po_path" ${1+"$@"} </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.
>         "$po_path" ${1+"$@"} </dev/null 2>&1 &
>     fi
>
>     # if had an old lockfile, check to see if we were able to start
>     if [ -n "$oldpid" ];then
>         sleep 1
>         if [ -f "$PIDFILE" ];then
>             if [ "`sed -n 1p $PIDFILE`" = "$oldpid" ];then
>                 echo "$CMDNAME: cannot start postmaster" 1>&2
>                 echo "Examine the log output." 1>&2
>                 exit 1
>             fi
>         fi
>     fi
>
>     # wait for postmaster to start
>     if [ "$wait" = yes ];then
>         cnt=0
>         $silence_echo $ECHO_N "waiting for postmaster to start..."$ECHO_C
>         while :
>         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
>             then
>                 break;
>             else
>                 $silence_echo $ECHO_N "."$ECHO_C
>                 cnt=`expr $cnt + 1`
>                 if [ "$cnt" -gt "$wait_seconds" ];then
>                     $silence_echo echo "failed"
>                     echo "$CMDNAME: postmaster does not start" 1>&2
>                     exit 1
>                 fi
>                 sleep 1
>             fi
>         done
>         $silence_echo echo "done"
>     fi
>     $silence_echo echo "postmaster successfully started"
> fi # start or restart
>
> exit 0
>
> __________________________________________________________________
> John Pagakis
> Email: john@pagakis.com
>
> Cheese -- Milk's leap towards immortality.
>        -- Clifton Fadiman
>
> This signature generated by
>      ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software,
Inc.
>                                               www.spazmodicfrog.com
>
>
> -----Original Message-----
> From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
> Sent: Wednesday, September 17, 2003 8:51 AM
> To: John Pagakis
> Cc: cygwin@cygwin.com
> Subject: RE: Problem with stopping postmaster with pg_ctl
>
>
> John,
>
> This turned out to be a problem with /bin/kill.exe in Cygwin 1.5.4, which
> should be fixed in the next release
> (<http://cygwin.com/ml/cygwin/2003-09/msg01101.html>).  If you're
> adventurous, try the snapshot.  Otherwise, a couple of quick workarounds
> until /bin/kill is fixed are to use different syntax or use bash's builtin
> kill.  For the former, change "kill -15 $PID" to "kill -s 15 $PID".  For
> the latter, either change the #! line in pg_ctl to "#!/bin/bash" instead
> of "#!/bin/sh", or force bash's kill by using 'bash -c "kill -15 $PID"'
> instead of "kill -15 $PID".
>
> Both of these will become unnecessary in the next Cygwin release, but
> won't hurt, and should keep you running until then.  Hope this helps,
>         Igor
>
> On Wed, 17 Sep 2003, John Pagakis wrote:
>
> > Oh, I meant /bin/kill not /etc/kill on that last post .....
> >
> > The way Postgres controls things is, when it starts up, it stores the
PID
> it
> > started under in a file called postgresql.pid in the data directory.
When
> > you use "pg_ctl stop", it reads the pid file and the issues a kill -15
on
> > that pid.  It then sits and waits for postgresql.pid to disappear.
After
> > one minute, if the file is still there, the script gives up and
announces
> > that the postmaster will not stop.
> >
> > If you try this, you'll notice that when you get to the kill in the
> script,
> > you'll get the Usage info on screen.  There is nothing wrong with how
> pg_ctl
> > is formatting the kill.  If you echo out that command and execute it
from
> > the command line, it works just fine.
> >
> > I believe the problem is Cygwin's implementation of kill.  From the
> command
> > line if you say kill -sig pid, it works.  If you say /bin/kill -sig pid
it
> > gives you Usage.  There is something about when you give the fully
> qualified
> > path that it finds offensive.  The script does not give the fully
> qualified
> > path, but based on the behavior, I'm guessing the interpreter resolves
to
> > the fully qualified path before executing.
> >
> > Anyway, BEFORE you exit, do this:
> >
> > 1) ps
> > This gives you a list of active processes.
> >
> > 2) Find the pid for postgres who's ppid is 1.
> >
> > 3) kill -15 that pid.
> >
> > 4) Wait for the message that the database is shut down.
> >
> > Now you can exit.
> >
> > __________________________________________________________________
> > John Pagakis
> > Email: john@pagakis.com
> >
> > "With all your science can you tell how it is, and whence it is, that
> >  light comes into the soul?"
> >         -- Henry David Thoreau
> >
> > This signature generated by
> >      ... and I Quote!!(tm) Copyright (c) 1999 SpaZmodic Frog Software,
> Inc.
> >                                               www.spazmodicfrog.com
> >
> >
> > -----Original Message-----
> > From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
> > Sent: Tuesday, September 16, 2003 1:47 PM
> > To: John Pagakis
> > Cc: pgsql-cygwin@postgresql.org; cygwin@cygwin.com
> > Subject: Re: Problem with stopping postmaster with pg_ctl
> >
> >
> > On Tue, 16 Sep 2003, John Pagakis wrote:
> >
> > > I'm trying to get Postgres working under Cygwin.  The good news is, it
> > > mostly is.  The bad news is, I can't shut it down with pg_ctl.
> > >
> > > I loaded the full Cygwin installation on my Win2K Pro machine, and
later
> > on
> > > my XP Pro box.  Both behave the same way.
> > >
> > > When I run pg_ctl stop, it motors for the duration of the wait period
> and
> > > then says the postmaster does not shut down.
> > >
> > > Upon further review ......
> > >
> > > It looks like pg_ctl looks through the process list for the Postgres
> > process
> > > and then tries to kill it.  You can specify the shutdown as smart
(which
> > > translates to kill -TERM), fast (kill -INT) or immediate (kill -QUIT).
> > >
> > > When the script hits the kill, I get Usage info on the screen!!  So,
the
> > > interpreter is not seeing this as a valid command line string for
kill.
> I
> > > have echoed the command being generated out and it looks fine.  I can
> take
> > > that same command and execute it: it shuts Postgres down.
> > >
> > > Why would the interpreter rejecting the command line for kill when it
> > > appears to be well formed?
> > >
> > > Any help would be greatly appreciated.
> >
> > You probably have another "kill" in the path before "/bin/kill".  Try
> > "bash -c 'exec -l sh'", and from there "which kill".
> >
> > Had you attached the output of "cygcheck -svr", as requested in the
> > problem reporting guidelines at <http://cygwin.com/problems.html>, it
> > would have provided some information for a more intelligent guess.
> >         Igor
>
> --
>                                 http://cs.nyu.edu/~pechtcha/
>       |\      _,,,---,,_                pechtcha@cs.nyu.edu
> ZZZzz /,`.-'`'    -.  ;-;;,_            igor@watson.ibm.com
>      |,4-  ) )-,_. ,\ (  `'-'           Igor Pechtchanski, Ph.D.
>     '---''(_/--'  `-'\_) fL     a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!
>
> "I have since come to realize that being between your mentor and his route
> to the bathroom is a major career booster."  -- Patrick Naughton
>
>
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Problem reports:       http://cygwin.com/problems.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
>

--
                http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_        pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_        igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'        Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL    a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"I have since come to realize that being between your mentor and his route
to the bathroom is a major career booster."  -- Patrick Naughton