]> git.pld-linux.org Git - packages/pound.git/blob - pound.init
- skip start/stop if no instances left over from restart config check
[packages/pound.git] / pound.init
1 #!/bin/sh
2 #
3 # pound
4 #
5 # chkconfig:    345 85 15
6 # description:  reverse-proxy and load-balancer
7 #
8
9 # Source function library
10 . /etc/rc.d/init.d/functions
11
12 # Get network config
13 . /etc/sysconfig/network
14
15 # List of instances to start.
16 POUND_INSTANCES="pound"
17
18 # Get service config
19 [ -f /etc/sysconfig/pound ] && . /etc/sysconfig/pound
20
21 # Check that networking is up.
22 if is_yes "${NETWORKING}"; then
23         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
24                 msg_network_down pound
25                 exit 1
26         fi
27 else
28         exit 0
29 fi
30
31 configtest() {
32         local instance="$1"
33         pound -c -f /etc/pound/$instance.cfg > /dev/null
34 }
35
36
37 # check if the $1 instance is up
38 is_up() {
39         local instance="$1"
40         local pidfile=/var/run/pound/$instance.pid
41         [ -f $pidfile ] || return 1
42         local pid=$(cat $pidfile)
43         kill -0 $pid 2>/dev/null
44         return $?
45 }
46
47 # check if any of the instances are up
48 any_up() {
49         local ret=1 instance pidfile
50
51         for pidfile in /var/run/pound/*.pid; do
52                 [ -f "$pidfile" ] || continue
53                 instance=${pidfile#/var/run/pound/}
54                 instance=${instance%.pid}
55                 is_up $instance || continue
56                 ret=0
57         done
58
59         return $ret
60 }
61
62 # check if all of the instances are up
63 all_up() {
64         local ret=0 instance pidfile
65
66         for pidfile in /var/run/pound/*.pid; do
67                 [ -f "$pidfile" ] || continue
68                 instance=${pidfile#/var/run/pound/}
69                 instance=${instance%.pid}
70                 is_up $instance && continue
71                 ret=1
72         done
73
74         return $ret
75 }
76
77 start() {
78         local ret started=0 found=0 instance
79
80         # Check if the service is already running?
81         if ! all_up; then
82                 msg_starting "Pound"; started
83                 for instance in $POUND_INSTANCES; do
84                         show "Starting Pound instance %s" "$instance"
85                         if is_up $instance; then
86                                 started
87                                 continue
88                         fi
89
90                         PIDFILE=/var/run/pound/$instance.pid
91                         start-stop-daemon --start \
92                                 --exec /usr/sbin/pound \
93                                 --pidfile $PIDFILE -- -v -f /etc/pound/$instance.cfg -p $PIDFILE
94                         ret=$?
95
96                         if [ $ret -eq 0 ]; then
97                                 ok
98                                 RETVAL=$ret
99                                 started=1
100                                 found=1
101                         else
102                                 fail
103                         fi
104                 done
105
106                 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/pound
107         else
108                 msg_already_running "Pound"
109         fi
110 }
111
112 stop() {
113         local ret instance
114         # Stop daemons.
115         if any_up; then
116                 msg_stopping "Pound"; started
117                 for instance in $POUND_INSTANCES; do
118                         is_up $instance || continue
119                         show "Stopping Pound instance %s" "$instance"; busy
120                         killproc --pidfile pound/$instance.pid pound
121                         ret=$?
122                 done
123                 rm -f /var/lock/subsys/pound > /dev/null 2>&1
124         else
125                 msg_not_running "Pound"
126         fi
127 }
128
129 restart() {
130         local instance
131
132         if any_up; then
133                 # make up list of configured and up instances
134                 local list
135                 show "Checking configuration"; busy
136                 for instance in $POUND_INSTANCES; do
137                         # skip ones whose config fails
138                         configtest $instance || continue
139                         list="$list $instance"
140                 done
141                 ok
142
143                 POUND_INSTANCES=$list
144
145                 # nothing left or never was
146                 [ "$list" ] || return
147         fi
148
149         stop
150         start
151 }
152
153 condrestart() {
154         if [ -f /var/lock/subsys/pound ]; then
155                 stop
156                 start
157         else
158                 msg_not_running "Pound"
159                 RETVAL=$1
160         fi
161 }
162
163 pound_status() {
164         local stat=1 pidfile instance
165
166         nls "Configured Pound instances:"
167         echo " $POUND_INSTANCES"
168         nls "Currently active Pound instances:"
169         for pidfile in /var/run/pound/*.pid; do
170                 [ -f "$pidfile" ] || continue
171                 instance=${pidfile#/var/run/pound/}
172                 instance=${instance%.pid}
173                 is_up $instance && echo -n " $instance($(cat $pidfile))"
174                 stat=0
175         done
176         echo ""
177         exit $stat
178 }
179
180 if [ "$1" != status -a "$2" ]; then
181         POUND_INSTANCES="$2"
182 fi
183
184 RETVAL=0
185 # See how we were called.
186 case "$1" in
187   start)
188         start
189         ;;
190   stop)
191         stop
192         ;;
193   restart)
194         restart
195         ;;
196   try-restart)
197         condrestart 0
198         ;;
199   force-reload)
200         condrestart 7
201         ;;
202   flush-logs)
203         if [ -f /var/lock/subsys/pound ]; then
204                 for instance in $POUND_INSTANCES; do
205                         show "Rotating Pound logs for %s instance" $instance
206                         killproc --pidfile /var/run/pound/$instance.pid pound -USR1
207                         ret=$?
208                         if [ $ret != 0 ]; then
209                                 RETVAL=$ret
210                         fi
211                 done
212         else
213                 msg_not_running "Pound"
214                 RETVAL=7
215         fi
216         ;;
217   status)
218         pound_status
219         ;;
220   *)
221         msg_usage "$0 {start|stop|restart|try-restart|force-reload|flush-logs|status} [INSTANCE NAMES]"
222         exit 3
223 esac
224
225 exit $RETVAL
This page took 0.063158 seconds and 4 git commands to generate.