summaryrefslogtreecommitdiff
path: root/src/redhat/pgpool.init
blob: 10924b02696b1c3e9185c4c41ee0e7ff6320b358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/sh
# pgpool    This is the init script for starting up pgpool-II
#
# chkconfig: - 64 36
# description: Starts and stops the pgpool daemon
# processname: pgpool
# pidfile:	/var/run/${NAME}.pid
#
# v1.0.0 Devrim GUNDUZ <devrim@CommandPrompt.com>
# - Initial version of Red Hat / Fedora init script
#
# v2.2 Devrim GUNDUZ <devrim@CommandPrompt.com>
# - New and improved version which has some fixes.
#
# v2.2.5 Devrim GUNDUZ <devrim@CommandPrompt.com>
# - Fix logging.
#
# v3.3.3 Ryan Deshone <rdshone@liquidweb.com> and Yugo Nagata <nagata@sraoss.co.jp>
# - Update stop and reload to use pgpool commands properly
# - Removed "switch" as pgpool no longer supports that command
# - Add try-restart option
# - Fix exit code according with LSB

# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions

# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`

# Get config.
. /etc/sysconfig/network

# Check that networking is up.
# We need it for pgpool
[ "${NETWORKING}" = "no" ] && exit 0

# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
	NAME=${NAME:3}
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
    SU=runuser
else
    SU=su
fi

# Set defaults for configuration variables
PGPOOLUSER=postgres
PGPOOLENGINE=/usr/bin
PGPOOLDAEMON=$PGPOOLENGINE/pgpool
PGPOOLCONF=/etc/pgpool-II/pgpool.conf
PGPOOLPIDDIR=/var/run/pgpool
PGPOOLLOG=/var/log/pgpool.log

lockfile="/var/lock/subsys/${NAME}"
pidfile="$PGPOOLPIDDIR/pgpool.pid"

# Import configuration from /etc/sysconfig, if it exists 
[ -f /etc/sysconfig/${NAME} ] && . /etc/sysconfig/${NAME}

test -x $PGPOOLDAEMON || exit 5

# Check whether the config file exists or not
if [ ! -r $PGPOOLCONF ]
then
	echo "$PGPOOLCONF not found"
	echo_failure
	echo
	exit 1
fi

# Create the log file if it does not exist
if [ ! -x $PGPOOLLOG ]
then
	touch $PGPOOLLOG
	chown ${PGPOOLUSER}: $PGPOOLLOG
fi

if [ ! -d $PGPOOLPIDDIR ]
then
	mkdir $PGPOOLPIDDIR
	chown ${PGPOOLUSER}: $PGPOOLPIDDIR
fi

script_result=0

start(){
	PGPOOL_START=$"Starting ${NAME} service: "

	echo -n "$PGPOOL_START"
	if [ -n "`pidofproc -p $pidfile $PGPOOLDAEMON`" ]
	then
		echo_success
		echo
		exit 0
	fi

	$SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF $OPTS & " >> "$PGPOOLLOG" 2>&1 < /dev/null
	sleep 2

	if [ -n "`pidofproc -p $pidfile $PGPOOLDAEMON`" ]
	then
		success "$PGPOOL_START"
		touch "$lockfile"
		echo
	else
		failure "$PGPOOL_START"
		echo
		script_result=1
	fi
}

stop(){
	PGPOOL_STOP=$"Stopping ${NAME} service: "

	echo -n "$PGPOOL_STOP"
	if [ -e "$lockfile" ]
	then
		$SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF -m fast stop" >> "$PGPOOLLOG" 2>&1 < /dev/null

		RETVAL=$?
		if [ $RETVAL -eq 0 ]
		then
			success "$PGPOOL_STOP"
			rm -f "$lockfile"
		else
			failure "$PGPOOL_STOP"
			script_result=1
		fi
	else
		echo_success
	fi
	echo
}

restart(){
    stop
    start
}

reload(){
	PGPOOL_RELOAD=$"Reloading ${NAME} configuration: "

	echo -n "$PGPOOL_RELOAD"
	if [ -n "`pidofproc -p $pidfile $PGPOOLDAEMON`" ]
	then
		$SU -l $PGPOOLUSER -c "$PGPOOLDAEMON -f $PGPOOLCONF reload" >> "$PGPOOLLOG" 2>&1 < /dev/null
	else
		failure "$PGPOOL_RELOAD"
		echo
		exit 1
	fi
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		success "$PGPOOL_RELOAD"
	else
		failure "$PGPOOL_RELOAD"
		script_result=1
	fi
	echo
}

condrestart(){
	[ -e "$lockfile" ] && restart
}

condstop(){
	[ -e "$lockfile" ] && stop
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p $pidfile pgpool
        script_result=$?
        ;;
  restart)
        restart
        ;;
  reload|force-reload)
        reload
        ;;
  condrestart|try-restart)
        condrestart
        ;;
  condstop)
        condstop
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|condstop|reload|force-reload}"
        exit 2
esac

exit $script_result