squid 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #! /bin/sh
  2. #
  3. # squid Startup script for the SQUID HTTP proxy-cache.
  4. #
  5. # Version: @(#)squid.rc 1.0 07-Jul-2006 luigi@debian.org
  6. #
  7. # pidfile: /run/squid.pid
  8. #
  9. ### BEGIN INIT INFO
  10. # Provides: squid
  11. # Required-Start: $network $remote_fs $syslog
  12. # Required-Stop: $network $remote_fs $syslog
  13. # Should-Start: $named
  14. # Should-Stop: $named
  15. # Default-Start: 2 3 4 5
  16. # Default-Stop: 0 1 6
  17. # Short-Description: Squid HTTP Proxy version 4.x
  18. ### END INIT INFO
  19. NAME=squid
  20. DESC="Squid HTTP Proxy"
  21. DAEMON=/usr/sbin/squid
  22. PIDFILE=/run/$NAME.pid
  23. CONFIG=/etc/squid/squid.conf
  24. SQUID_ARGS="-YC -f $CONFIG"
  25. [ ! -f /etc/default/squid ] || . /etc/default/squid
  26. . /lib/lsb/init-functions
  27. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  28. [ -x $DAEMON ] || exit 0
  29. ulimit -n 65535
  30. find_cache_dir () {
  31. w=" " # space tab
  32. res=`$DAEMON -k parse -f $CONFIG 2>&1 |
  33. grep "Processing:" |
  34. sed s/.*Processing:\ // |
  35. sed -ne '
  36. s/^['"$w"']*'$1'['"$w"']\+[^'"$w"']\+['"$w"']\+\([^'"$w"']\+\).*$/\1/p;
  37. t end;
  38. d;
  39. :end q'`
  40. [ -n "$res" ] || res=$2
  41. echo "$res"
  42. }
  43. grepconf () {
  44. w=" " # space tab
  45. res=`$DAEMON -k parse -f $CONFIG 2>&1 |
  46. grep "Processing:" |
  47. sed s/.*Processing:\ // |
  48. sed -ne '
  49. s/^['"$w"']*'$1'['"$w"']\+\([^'"$w"']\+\).*$/\1/p;
  50. t end;
  51. d;
  52. :end q'`
  53. [ -n "$res" ] || res=$2
  54. echo "$res"
  55. }
  56. create_run_dir () {
  57. run_dir=/run/squid
  58. usr=`grepconf cache_effective_user proxy`
  59. grp=`grepconf cache_effective_group proxy`
  60. if [ "$(dpkg-statoverride --list $run_dir)" = "" ] &&
  61. [ ! -e $run_dir ] ; then
  62. mkdir -p $run_dir
  63. chown $usr:$grp $run_dir
  64. [ -x /sbin/restorecon ] && restorecon $run_dir
  65. fi
  66. }
  67. start () {
  68. cache_dir=`find_cache_dir cache_dir`
  69. cache_type=`grepconf cache_dir`
  70. run_dir=/run/squid
  71. #
  72. # Create run dir (needed for several workers on SMP)
  73. #
  74. create_run_dir
  75. #
  76. # Create spool dirs if they don't exist.
  77. #
  78. if test -d "$cache_dir" -a ! -d "$cache_dir/00"
  79. then
  80. log_warning_msg "Creating $DESC cache structure"
  81. $DAEMON -z --foreground -f $CONFIG
  82. [ -x /sbin/restorecon ] && restorecon -R $cache_dir
  83. fi
  84. umask 027
  85. ulimit -n 65535
  86. cd $run_dir
  87. start-stop-daemon --quiet --start \
  88. --pidfile $PIDFILE \
  89. --exec $DAEMON -- $SQUID_ARGS < /dev/null
  90. return $?
  91. }
  92. stop () {
  93. PID=`cat $PIDFILE 2>/dev/null`
  94. start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
  95. #
  96. # Now we have to wait until squid has _really_ stopped.
  97. #
  98. sleep 2
  99. if test -n "$PID" && kill -0 $PID 2>/dev/null
  100. then
  101. log_action_begin_msg " Waiting"
  102. cnt=0
  103. while kill -0 $PID 2>/dev/null
  104. do
  105. cnt=`expr $cnt + 1`
  106. if [ $cnt -gt 24 ]
  107. then
  108. log_action_end_msg 1
  109. return 1
  110. fi
  111. sleep 5
  112. log_action_cont_msg ""
  113. done
  114. log_action_end_msg 0
  115. return 0
  116. else
  117. return 0
  118. fi
  119. }
  120. cfg_pidfile=`grepconf pid_filename`
  121. if test "${cfg_pidfile:-none}" != "none" -a "$cfg_pidfile" != "$PIDFILE"
  122. then
  123. log_warning_msg "squid.conf pid_filename overrides init script"
  124. PIDFILE="$cfg_pidfile"
  125. fi
  126. case "$1" in
  127. start)
  128. res=`$DAEMON -k parse -f $CONFIG 2>&1 | grep -o "FATAL: .*"`
  129. if test -n "$res";
  130. then
  131. log_failure_msg "$res"
  132. exit 3
  133. else
  134. log_daemon_msg "Starting $DESC" "$NAME"
  135. if start ; then
  136. log_end_msg $?
  137. else
  138. log_end_msg $?
  139. fi
  140. fi
  141. ;;
  142. stop)
  143. log_daemon_msg "Stopping $DESC" "$NAME"
  144. if stop ; then
  145. log_end_msg $?
  146. else
  147. log_end_msg $?
  148. fi
  149. ;;
  150. reload|force-reload)
  151. res=`$DAEMON -k parse -f $CONFIG 2>&1 | grep -o "FATAL: .*"`
  152. if test -n "$res";
  153. then
  154. log_failure_msg "$res"
  155. exit 3
  156. else
  157. log_action_msg "Reloading $DESC configuration files"
  158. start-stop-daemon --stop --signal 1 \
  159. --pidfile $PIDFILE --quiet --exec $DAEMON
  160. log_action_end_msg 0
  161. fi
  162. ;;
  163. restart)
  164. res=`$DAEMON -k parse -f $CONFIG 2>&1 | grep -o "FATAL: .*"`
  165. if test -n "$res";
  166. then
  167. log_failure_msg "$res"
  168. exit 3
  169. else
  170. log_daemon_msg "Restarting $DESC" "$NAME"
  171. stop
  172. if start ; then
  173. log_end_msg $?
  174. else
  175. log_end_msg $?
  176. fi
  177. fi
  178. ;;
  179. status)
  180. status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit 3
  181. ;;
  182. *)
  183. echo "Usage: /etc/init.d/$NAME {start|stop|reload|force-reload|restart|status}"
  184. exit 3
  185. ;;
  186. esac
  187. exit 0