1#!/bin/sh
2# pgpool    This is the init script for starting up pgpool-II
3#
4# chkconfig: - 64 36
5# description: Starts and stops the pgpool daemon
6# processname: pgpool
7# pidfile:	/var/run/${NAME}.pid
8#
9# v1.0.0 Devrim GUNDUZ <devrim@CommandPrompt.com>
10# - Initial version of Red Hat / Fedora init script
11#
12# v2.2 Devrim GUNDUZ <devrim@CommandPrompt.com>
13# - New and improved version which has some fixes.
14#
15# v2.2.5 Devrim GUNDUZ <devrim@CommandPrompt.com>
16# - Fix logging.
17#
18# v3.3.3 Ryan Deshone <rdshone@liquidweb.com> and Yugo Nagata <nagata@sraoss.co.jp>
19# - Update stop and reload to use pgpool commands properly
20# - Removed "switch" as pgpool no longer suppports that command
21# - Add try-restart option
22# - Fix exit code according with LSB
23
24# Source function library.
25INITD=/etc/rc.d/init.d
26. $INITD/functions
27
28# Get function listing for cross-distribution logic.
29TYPESET=`typeset -f|grep "declare"`
30
31# Get config.
32. /etc/sysconfig/network
33
34# Check that networking is up.
35# We need it for pgpool
36[ "${NETWORKING}" = "no" ] && exit 0
37
38# Find the name of the script
39NAME=`basename $0`
40if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
41then
42	NAME=${NAME:3}
43fi
44
45# For SELinux we need to use 'runuser' not 'su'
46if [ -x /sbin/runuser ]
47then
48    SU=runuser
49else
50    SU=su
51fi
52
53# Set defaults for configuration variables
54PGPOOLUSER=postgres
55PGPOOLENGINE=/usr/bin
56PGPOOLDAEMON=$PGPOOLENGINE/pgpool
57PGPOOLCONF=/etc/pgpool-II/pgpool.conf
58PGPOOLPIDDIR=/var/run/pgpool
59PGPOOLLOG=/var/log/pgpool.log
60
61lockfile="/var/lock/subsys/${NAME}"
62pidfile="$PGPOOLPIDDIR/pgpool.pid"
63
64# Import configuration from /etc/sysconfig, if it exists
65[ -f /etc/sysconfig/${NAME} ] && . /etc/sysconfig/${NAME}
66
67test -x $PGPOOLDAEMON || exit 5
68
69# Check whether the config file exists or not
70if [ ! -r $PGPOOLCONF ]
71then
72	echo "$PGPOOLCONF not found"
73	echo_failure
74	echo
75	exit 1
76fi
77
78# Create the log file if it does not exist
79if [ ! -x $PGPOOLLOG ]
80then
81	touch $PGPOOLLOG
82	chown ${PGPOOLUSER}: $PGPOOLLOG
83fi
84
85if [ ! -d $PGPOOLPIDDIR ]
86then
87	mkdir $PGPOOLPIDDIR
88	chown ${PGPOOLUSER}: $PGPOOLPIDDIR
89fi
90
91script_result=0
92
93start(){
94	PGPOOL_START=$"Starting ${NAME} service: "
95
96	echo -n "$PGPOOL_START"
97	if [ -n "`pidofproc -p $pidfile $PGPOOLDAEMON`" ]
98	then
99		echo_success
100		echo
101		exit 0
102	fi
103
104	$SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF $OPTS & " >> "$PGPOOLLOG" 2>&1 < /dev/null
105	sleep 2
106
107	if [ -n "`pidofproc -p $pidfile $PGPOOLDAEMON`" ]
108	then
109		success "$PGPOOL_START"
110		touch "$lockfile"
111		echo
112	else
113		failure "$PGPOOL_START"
114		echo
115		script_result=1
116	fi
117}
118
119stop(){
120	PGPOOL_STOP=$"Stopping ${NAME} service: "
121
122	echo -n "$PGPOOL_STOP"
123	if [ -e "$lockfile" ]
124	then
125		$SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF -m fast stop" >> "$PGPOOLLOG" 2>&1 < /dev/null
126
127		RETVAL=$?
128		if [ $RETVAL -eq 0 ]
129		then
130			success "$PGPOOL_STOP"
131			rm -f "$lockfile"
132		else
133			failure "$PGPOOL_STOP"
134			script_result=1
135		fi
136	else
137		echo_success
138	fi
139	echo
140}
141
142restart(){
143    stop
144    start
145}
146
147reload(){
148	PGPOOL_RELOAD=$"Reloading ${NAME} configuration: "
149
150	echo -n "$PGPOOL_RELOAD"
151	if [ -n "`pidofproc -p $pidfile $PGPOOLDAEMON`" ]
152	then
153		$SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF reload" >> "$PGPOOLLOG" 2>&1 < /dev/null
154	else
155		failure "$PGPOOL_RELOAD"
156		echo
157		exit 1
158	fi
159	RETVAL=$?
160	if [ $RETVAL -eq 0 ]; then
161		success "$PGPOOL_RELOAD"
162	else
163		failure "$PGPOOL_RELOAD"
164		script_result=1
165	fi
166	echo
167}
168
169condrestart(){
170	[ -e "$lockfile" ] && restart
171}
172
173condstop(){
174	[ -e "$lockfile" ] && stop
175}
176
177# See how we were called.
178case "$1" in
179  start)
180        start
181        ;;
182  stop)
183        stop
184        ;;
185  status)
186        status -p $pidfile pgpool
187        script_result=$?
188        ;;
189  restart)
190        restart
191        ;;
192  reload|force-reload)
193        reload
194        ;;
195  condrestart|try-restart)
196        condrestart
197        ;;
198  condstop)
199        condstop
200        ;;
201  *)
202        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|condstop|reload|force-reload}"
203        exit 2
204esac
205
206exit $script_result
207