Advertisement
Guest User

Untitled

a guest
May 28th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: haproxy
  4. # Required-Start: $local_fs $network $remote_fs $syslog
  5. # Required-Stop: $local_fs $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: fast and reliable load balancing reverse proxy
  9. # Description: This file should be used to start and stop haproxy.
  10. ### END INIT INFO
  11.  
  12. # Author: Arnaud Cornet <acornet@debian.org>
  13.  
  14. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  15. PIDFILE=/var/run/haproxy.pid
  16. CONFIGDIR=/etc/haproxy/conf.d/
  17. TMPCONFIG=/tmp/haproxy.tmp.conf
  18. HAPROXY=/usr/sbin/haproxy
  19. RUNDIR=/run/haproxy
  20. EXTRAOPTS=
  21.  
  22. test -x $HAPROXY || exit 0
  23.  
  24. if [ -e /etc/default/haproxy ]; then
  25. . /etc/default/haproxy
  26. fi
  27.  
  28. test -d "$CONFIGDIR" || exit 0
  29.  
  30. [ -f /etc/default/rcS ] && . /etc/default/rcS
  31. . /lib/lsb/init-functions
  32.  
  33.  
  34. check_haproxy_config()
  35. {
  36. files=`find /etc/haproxy/conf.d/ -name *.conf -printf "%p\n"`
  37. while read line; do
  38. cat $line >> "$TMPCONFIG"
  39. done <<< """$files"""
  40.  
  41. $HAPROXY -c -f "$TMPCONFIG" >/dev/null
  42. if [ $? -eq 1 ]; then
  43. log_end_msg 1
  44. exit 1
  45. fi
  46. rm "$TMPCONFIG"
  47. }
  48.  
  49. haproxy_start()
  50. {
  51. [ -d "$RUNDIR" ] || mkdir "$RUNDIR"
  52. chown haproxy:haproxy "$RUNDIR"
  53. chmod 2775 "$RUNDIR"
  54.  
  55. check_haproxy_config
  56. files=`find /etc/haproxy/conf.d/ -name *.conf -printf " -f %p"`
  57. start-stop-daemon --quiet --oknodo --start --pidfile "$PIDFILE" \
  58. --exec $HAPROXY -- $files -D -p "$PIDFILE" \
  59. $EXTRAOPTS || return 2
  60. return 0
  61. }
  62.  
  63. haproxy_stop()
  64. {
  65. if [ ! -f $PIDFILE ] ; then
  66. # This is a success according to LSB
  67. return 0
  68. fi
  69. for pid in $(cat $PIDFILE) ; do
  70. if kill -0 $pid 2>/dev/null; then
  71. /bin/kill $pid || return 4
  72. fi
  73. done
  74. rm -f $PIDFILE
  75. return 0
  76. }
  77.  
  78. haproxy_reload()
  79. {
  80. check_haproxy_config
  81. files=`find /etc/haproxy/conf.d/ -name *.conf -printf "-f %p "`
  82. $HAPROXY "$files" -p $PIDFILE -D $EXTRAOPTS -sf $(cat $PIDFILE) \
  83. || return 2
  84. return 0
  85. }
  86.  
  87. haproxy_status()
  88. {
  89. if [ ! -f $PIDFILE ] ; then
  90. # program not running
  91. return 3
  92. fi
  93.  
  94. for pid in $(cat $PIDFILE) ; do
  95. if ! ps --no-headers p "$pid" | grep haproxy > /dev/null ; then
  96. # program running, bogus pidfile
  97. return 1
  98. fi
  99. done
  100.  
  101. return 0
  102. }
  103.  
  104.  
  105. case "$1" in
  106. start)
  107. log_daemon_msg "Starting haproxy" "haproxy"
  108. haproxy_start
  109. ret=$?
  110. case "$ret" in
  111. 0)
  112. log_end_msg 0
  113. ;;
  114. 1)
  115. log_end_msg 1
  116. echo "pid file '$PIDFILE' found, haproxy not started."
  117. ;;
  118. 2)
  119. log_end_msg 1
  120. ;;
  121. esac
  122. exit $ret
  123. ;;
  124. stop)
  125. log_daemon_msg "Stopping haproxy" "haproxy"
  126. haproxy_stop
  127. ret=$?
  128. case "$ret" in
  129. 0|1)
  130. log_end_msg 0
  131. ;;
  132. 2)
  133. log_end_msg 1
  134. ;;
  135. esac
  136. exit $ret
  137. ;;
  138. reload|force-reload)
  139. log_daemon_msg "Reloading haproxy" "haproxy"
  140. haproxy_reload
  141. ret=$?
  142. case "$ret" in
  143. 0|1)
  144. log_end_msg 0
  145. ;;
  146. 2)
  147. log_end_msg 1
  148. ;;
  149. esac
  150. exit $ret
  151. ;;
  152. restart)
  153. log_daemon_msg "Restarting haproxy" "haproxy"
  154. haproxy_stop
  155. haproxy_start
  156. ret=$?
  157. case "$ret" in
  158. 0)
  159. log_end_msg 0
  160. ;;
  161. 1)
  162. log_end_msg 1
  163. ;;
  164. 2)
  165. log_end_msg 1
  166. ;;
  167. esac
  168. exit $ret
  169. ;;
  170. status)
  171. haproxy_status
  172. ret=$?
  173. case "$ret" in
  174. 0)
  175. echo "haproxy is running."
  176. ;;
  177. 1)
  178. echo "haproxy dead, but $PIDFILE exists."
  179. ;;
  180. *)
  181. echo "haproxy not running."
  182. ;;
  183. esac
  184. exit $ret
  185. ;;
  186. *)
  187. echo "Usage: /etc/init.d/haproxy {start|stop|reload|restart|status}"
  188. exit 2
  189. ;;
  190. esac
  191.  
  192. :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement