Index: contrib/start-scripts/linux-lsb =================================================================== RCS file: contrib/start-scripts/linux-lsb diff -N contrib/start-scripts/linux-lsb *** /dev/null 1 Jan 1970 00:00:00 -0000 --- contrib/start-scripts/linux-lsb 31 Aug 2009 19:02:11 -0000 *************** *** 0 **** --- 1,298 ---- + #! /bin/sh + + ### BEGIN INIT INFO + # Provides: postgresql + # Required-Start: $local_fs $network $syslog + # Should-Start: $remote_fs $named $time + # Required-Stop: $local_fs $network $syslog + # Should-Stop: $remote_fs $named + # Default-Start: 2 3 4 5 + # Default-Stop: 0 1 6 + # Short-Description: PostgreSQL RDBMS + # Description: PostgreSQL RDBMS service. + # The world's most advanced open source database. + # See http://www.postgresql.org/ for more information. + ### END INIT INFO + + # This is an example of a Linux LSB conforming init script. + # See http://refspecs.freestandards.org/ for more information on LSB. + + # Original author: Kevin Grittner + + # $PostgreSQL$ + + #-------------------------------------------------------------------- + # The only edits needed should be in the INIT INFO block above + # and between the lines of dashes below. If any other + # changes are needed, or you find a way to enhance the script, + # consider posting to the PostgreSQL hackers list. + #-------------------------------------------------------------------- + + # Installation prefix + prefix=/usr/local/pgsql + + # Data directory + PGDATA="/var/local/pgsql/data" + + # Who to run the postmaster as, usually "postgres". (NOT "root") + PGUSER=postgres + + # Where to keep a log file + PGLOG="$PGDATA/serverlog" + + #-------------------------------------------------------------------- + + # The path that is to be used for the script + PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + + # The LSB functions must be present. + lsbf=/lib/lsb/init-functions + test -r "$lsbf" || { + echo "$0: not able to read $lsbf: script cannot run" 1>&2 + exit 5 + } + + # Source the functions. + . "$lsbf" + # All output from the script should be through the LSB msg functions after this. + + # Define usage string, used in more than one place. + usage="Usage: $0 {start|stop|restart|try-restart|reload|force-reload|status}" + + # Check that we have one parameter: action + if [ $# -ne 1 ] ; then + if [ $# -lt 1 -o "$1" = "" ] ] ; then + log_failure_msg "$0: action not specified" + else + log_failure_msg "$0: too many parameters" + fi + log_warning_msg "$usage" + exit 2 + fi + action="$1" + + # What to use to manipulate the postmaster. + PGCTL="$prefix/bin/pg_ctl" + + # Only start if we can find pg_ctl. + test -x "$PGCTL" || { + if [ "$action" = "stop" ] ; then + log_warning_msg "$0: executable $PGCTL not found: $action request ignored" + exit 0 + else + log_failure_msg "$0: executable $PGCTL not found: $action request failed" + exit 5 + fi + } + + pidfile="$PGDATA/postmaster.pid" + servicename=$( basename "$0" ) + daemon="$prefix/bin/postgres" + + pg_initd_start () { + echo -n "Starting $servicename: " + su -c ". '$lsbf' ; start_daemon -p '$pidfile' '$PGCTL' -w -D '$PGDATA' -l '$PGLOG' start" - $PGUSER + rc=$? + } + + pg_initd_stop () { + pidlist=$( pidofproc -p "$pidfile" "$daemon" ) + if [ "$pidlist" = "" ] ; then + # If this happens, the process went away after the initial check. + echo "$servicename: not running" + rc=0 + return + fi + echo -n "Shutting down $servicename: " + su -c ". \"$lsbf\" ; killproc -p '$pidfile' '$daemon' SIGINT" - $PGUSER + echo -n 'waiting for server to stop...' + rc=1 + # Try "fast" shutdown for a while. + for seconds in $( seq 50 ) ; do + echo -n '.' + if ! ps -o pid= -p "$pidlist" >/dev/null ; then + rc=0 + break + fi + sleep 1 + done + # Fast didn't do it; try immediate shutdown. + if [ $rc -ne 0 ] ; then + su -c ". \"$lsbf\" ; killproc -p '$pidfile' '$daemon' SIGQUIT" - $PGUSER + for seconds in $( seq 10 ) ; do + echo -n '!' + if ! ps -o pid= -p "$pidlist" >/dev/null ; then + rc=0 + break + fi + sleep 1 + done + fi + ! ps -o pid= -p "$pidlist" >/dev/null + rc=$? + if [ "$rc" -eq 0 ] ; then + echo ' done' + rm -f "$pidfile" + else + echo ' failed' + fi + } + + pg_initd_reload () { + su -c "$PGCTL reload -D '$PGDATA'" - $PGUSER + rc=$? + } + + pg_initd_status () { + if [ ! -f "$pidfile" ] ; then + rc=3 + else + su -c ". \"$lsbf\" ; pidofproc -p '$pidfile' '$daemon'" - $PGUSER 1>/dev/null + rc=$? + if [ $rc -eq 0 ] ; then + su -c ". \"$lsbf\" ; $PGCTL status -D '$PGDATA'" - $PGUSER + rc=$? + if [ $rc -ne 0 ] ; then + # pg_ctl doesn't return LSB conforming values; treat non-success as "unknown" + rc=4 + fi + fi + fi + } + + pg_initd_exit () { + if [ "$action" = "status" ] ; then + case $rc in + 0) + log_success_msg "$servicename $action: running" + ;; + 1) + log_failure_msg "$servicename $action: dead with existing pid file: $pidfile" + ;; + 3) + log_failure_msg "$servicename $action: not running" + ;; + *) + log_failure_msg "$servicename $action: unknown ($rc)" + ;; + esac + else + case $rc in + 0) + log_success_msg "$servicename $action: done" + ;; + 1) + log_failure_msg "$servicename $action: failed" + ;; + 4) + log_failure_msg "$servicename $action: insufficient privilege" + ;; + 7) + log_failure_msg "$servicename $action: not running" + ;; + *) + log_failure_msg "$servicename $action: failed ($rc)" + ;; + esac + fi + exit $rc + } + + # Actions other than status may use these return codes: + # 1 - generic or unspecified error + # 2 - invalid or excess argument(s) + # 3 - unimplemented feature (for example, "reload") + # 4 - user had insufficient privilege + # 5 - program is not installed + # 6 - program is not configured + # 7 - program is not running + # Some of these are tested before getting to this case statement. + case "$action" in + start) + # Start the service. + # If already running, return success without start attempt. + pg_initd_status + if [ $rc -eq 0 ] ; then + log_warning_msg "$servicename $action: service already running; no action taken" + pg_initd_exit + fi + pg_initd_start + pg_initd_exit + ;; + stop) + # Stop the service. + # If not running, return success without stop attempt. + pg_initd_status + if [ $rc -eq 3 ] ; then + log_warning_msg "$servicename $action: service not running; no action taken" + rc=0 + pg_initd_exit + fi + pg_initd_stop + pg_initd_exit + ;; + restart) + # Stop and restart the service if the service is already running, + # otherwise start the service. + pg_initd_status + if [ $rc -eq 3 ] ; then + log_warning_msg "$servicename $action: service not running; no attempt to stop" + else + pg_initd_stop + if [ $rc -ne 0 ] ; then + pg_initd_exit + fi + fi + pg_initd_start + pg_initd_exit + ;; + try-restart) + # Restart the service if the service is already running. + # If stopped, return success without stop attempt. + pg_initd_status + if [ $rc -eq 3 ] ; then + log_warning_msg "$servicename $action: service not running; no action taken" + rc=0 + pg_initd_exit + fi + if [ $rc -ne 0 ] ; then + pg_initd_exit + fi + pg_initd_stop + if [ $rc -ne 0 ] ; then + pg_initd_exit + fi + pg_initd_start + pg_initd_exit + ;; + reload|force-reload) + # Cause the configuration of the service to be reloaded + # without actually stopping and restarting the service. + # (Since PostgreSQL supports reload, force-reload should use that.) + pg_initd_status + if [ $rc -eq 3 ] ; then + rc=7 + pg_initd_exit + fi + pg_initd_reload + pg_initd_exit + ;; + status) + # Print the current status of the service. + # Return codes differ from other actions: + # 0 - program is running or service is OK + # 1 - program is dead and /var/run pid file exists + # 2 - program is dead and /var/lock lock file exists + # 3 - program is not running + # 4 - program or service status is unknown + pg_initd_status + pg_initd_exit + ;; + *) + # If we don't recognize action, consider it an invalid argument. + # If the standard adds actions we don't support, exit should be 3 for those. + log_failure_msg "$0: action \"$action\" not recognized" + log_warning_msg "$usage" + exit 2 + ;; + esac