Skip to content

Instantly share code, notes, and snippets.

@daniilyar
Created March 20, 2015 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniilyar/ea84189f540e488054f8 to your computer and use it in GitHub Desktop.
Save daniilyar/ea84189f540e488054f8 to your computer and use it in GitHub Desktop.
Debian /etc/init.d launch script for pgpool. Tested on Ubuntu 14.04 LTS (at Amazon EC2)
#! /bin/sh
### BEGIN INIT INFO
# Provides: pgpool2
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: postgresql
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start pgpool-II
# Description: pgpool-II is a connection pool server and replication
# proxy for PostgreSQL.
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/pgpool
DAEMONUSER=${DAEMONUSER:-postgres}
PIDFILE=/var/run/postgresql/pgpool.pid
test -x $DAEMON || exit 5
# Include pgpool defaults if available
if [ -f /etc/default/pgpool2 ] ; then
. /etc/default/pgpool2
fi
OPTS=""
if [ x"$PGPOOL_LOG_DEBUG" = x"yes" ]; then
OPTS="$OPTS -d"
fi
. /lib/lsb/init-functions
# This verifies the existence of numactl as well as testing that the command works
NUMACTL_ARGS="--interleave=all"
if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
then
NUMACTL="`which numactl` -- $NUMACTL_ARGS"
DAEMON_OPTS=${DAEMON_OPTS:-"-n"}
else
NUMACTL=""
DAEMON_OPTS="-- "${DAEMON_OPTS:-"-n"}
fi
is_running() {
# Check if the process is running looking at /proc
# (works for all users)
# No pidfile, probably no daemon present
[ ! -f "$PIDFILE" ] && return 1
pid=`cat $PIDFILE`
running_pid $pid $DAEMON || return 1
return 0
}
running_pid() {
# Check if a given process pid's cmdline matches a given name
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
# Is this the expected server
[ "$cmd" != "$name" ] && return 1
return 0
}
d_start() {
# Start the process using the wrapper
sudo -u $DAEMONUSER start-stop-daemon --background --start \
--pidfile $PIDFILE \
--make-pidfile --chuid $DAEMONUSER \
--exec $NUMACTL $DAEMON $DAEMON_OPTS
errcode=$?
return $errcode
}
d_stop() {
start-stop-daemon --stop --pidfile $PIDFILE \
--retry 300 \
--user $DAEMONUSER \
--exec $DAEMON
errcode=$?
return $errcode
}
case "$1" in
start)
log_daemon_msg "Starting pgpool-II" pgpool
d_start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping pgpool-II" pgpool
d_stop
log_end_msg $?
;;
status)
is_running
status=$?
if [ $status -eq 0 ]; then
log_success_msg "pgpool-II is running."
else
log_failure_msg "pgpool-II is not running."
fi
exit $status
;;
restart|force-reload)
log_daemon_msg "Restarting pgpool-II" pgpool
d_stop && sleep 1 && d_start
log_end_msg $?
;;
try-restart)
if $0 status >/dev/null; then
$0 restart
else
exit 0
fi
;;
reload)
exit 3
;;
*)
log_failure_msg "Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment