initscript.in 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/bin/bash
  2. #
  3. # @package_name@ This starts and stops @package_name@
  4. #
  5. # chkconfig: - 21 79
  6. # description: @package_name@ Directory Server
  7. # processname: @sbindir@/ns-slapd
  8. # configdir: @sysconfdir@/@package_name@/
  9. # piddir: @localstatedir@/run/@package_name@
  10. # datadir: @localstatedir@/lib/@package_name@/slapd-<instance name>
  11. #
  12. # Source function library.
  13. . /etc/rc.d/init.d/functions
  14. # Source networking configuration.
  15. . /etc/sysconfig/network
  16. # Check that networking is up.
  17. if [ ${NETWORKING} = "no" ]
  18. then
  19. echo "Networking is down"
  20. exit 0
  21. fi
  22. exec="@sbindir@/ns-slapd"
  23. prog="@package_name@"
  24. # Lockfile
  25. lockfile="@localstatedir@/lock/subsys/@package_name@"
  26. # PID directory
  27. piddir="@localstatedir@/run/@package_name@"
  28. # Instance basedir
  29. instbase="@instconfigdir@"
  30. [ -f $exec ] || exit 0
  31. umask 077
  32. pids=$(pidof $exec)
  33. INSTANCES=""
  34. for FILE in `/bin/ls -d $instbase/slapd-* 2>/dev/null`; do
  35. if [ -d "$FILE" ] ; then
  36. INSTANCES+=$(echo "$FILE" | sed -e "s|$instbase/slapd-||")
  37. INSTANCES+=" "
  38. fi
  39. done
  40. if [ -n "$2" ]; then
  41. for I in $INSTANCES; do
  42. if [ "$2" == "$I" ]; then
  43. INSTANCES="$2"
  44. fi
  45. done
  46. if [ "$2" != "$INSTANCES" ]; then
  47. echo -n "$2 is an invalid @package_name@ instance"
  48. failure; echo
  49. exit 1
  50. fi
  51. fi
  52. start() {
  53. if [ -n "$INSTANCES" ]; then
  54. export LD_LIBRARY_PATH=@libdir@/@package_name@:@nss_libdir@
  55. echo "Starting $prog: "
  56. # Start every slapd instance that isn't already running
  57. errors=0
  58. successes=0
  59. for instance in $INSTANCES; do
  60. echo -n " $instance..."
  61. # the server creates pidfile and writes the pid to it when it is fully
  62. # started and available to serve clients
  63. pidfile=$piddir/slapd-$instance.pid
  64. # the server creates startpidfile and writes the pid to just after
  65. # the process begins i.e. it received the startup request and didn't
  66. # die a horrible death (e.g. shared lib problem, oom, etc.)
  67. startpidfile=$piddir/slapd-$instance.startpid
  68. server_running=0
  69. if [ -e $pidfile ]; then
  70. pid=$(cat $pidfile)
  71. if [ $(echo "$pids" | grep -c $pid) -ge 1 ]; then
  72. echo -n " already running"
  73. success; echo
  74. let successes=successes+1
  75. server_running=1
  76. else
  77. echo -n " not running, but pid file exists - attempt to start anyway..."
  78. rm -f $pidfile
  79. fi
  80. fi
  81. server_started=0
  82. if [ $server_running -eq 0 ] ; then
  83. rm -f $pidfile
  84. rm -f $startpidfile
  85. $exec -D $instbase/slapd-$instance -i $pidfile -w $startpidfile
  86. if [ $? -eq 0 ]; then
  87. server_started=1 # well, perhaps not running, but started ok
  88. else
  89. failure; echo
  90. let errors=errors+1
  91. fi
  92. fi
  93. # ok, if we started the server successfully, let's see if it is really
  94. # running and ready to serve requests
  95. if [ $server_started -eq 1 ] ; then
  96. loop_counter=1
  97. # wait for 10 seconds for the start pid file to appear
  98. max_count=10
  99. while test $loop_counter -le $max_count; do
  100. loop_counter=`expr $loop_counter + 1`
  101. if test ! -f $startpidfile ; then
  102. sleep 1
  103. else
  104. pid=`cat $startpidfile`
  105. fi
  106. done
  107. if test ! -f $startpidfile ; then
  108. failure; echo
  109. let errors=errors+1
  110. server_started=0
  111. fi
  112. fi
  113. # ok, server wrote the startpid file - let's see if it comes up
  114. # ready to service requests
  115. if [ $server_started -eq 1 ] ; then
  116. loop_counter=1
  117. # wait for 10 minutes (600 times 1 seconds)
  118. max_count=600
  119. while test $loop_counter -le $max_count ; do
  120. loop_counter=`expr $loop_counter + 1`
  121. if test ! -f $pidfile ; then
  122. if kill -0 $pid > /dev/null 2>&1 ; then
  123. sleep 1
  124. else
  125. break
  126. fi
  127. else
  128. pid=`cat $pidfile`
  129. break
  130. fi
  131. done
  132. if kill -0 $pid > /dev/null 2>&1 && test -f $pidfile ; then
  133. success; echo
  134. let successes=successes+1
  135. else
  136. failure; echo
  137. let errors=errors+1
  138. fi
  139. fi
  140. rm -f $startpidfile
  141. done
  142. if [ $successes -ge 1 ]; then
  143. touch $lockfile
  144. fi
  145. if [ $errors -ge 1 ]; then
  146. echo "*** Warning: $errors instance(s) failed to start"
  147. fi
  148. else
  149. echo "*** Error: no $prog instances configured"
  150. fi
  151. }
  152. stop() {
  153. echo "Shutting down $prog: "
  154. errors=0
  155. for instance in $INSTANCES; do
  156. pidfile=$piddir/slapd-$instance.pid
  157. if [ -e $pidfile ]; then
  158. pid=$(cat $pidfile)
  159. echo -n " $instance..."
  160. server_stopped=0
  161. if [ $(echo "$pids" | grep -c $pid) -ge 1 ]; then
  162. kill $pid
  163. if [ $? -eq 0 ]; then
  164. server_stopped=1
  165. else
  166. failure; echo
  167. let errors=errors+1
  168. fi
  169. fi
  170. if [ $server_stopped -eq 1 ] ; then
  171. loop_counter=1
  172. # wait for 10 minutes (600 times 1 second)
  173. max_count=600
  174. while test $loop_counter -le $max_count; do
  175. loop_counter=`expr $loop_counter + 1`
  176. if kill -0 $pid > /dev/null 2>&1 ; then
  177. sleep 1
  178. else
  179. if test -f $pidfile ; then
  180. rm -f $pidfile
  181. fi
  182. break
  183. fi
  184. done
  185. if test -f $pidfile ; then
  186. failure; echo
  187. let errors=errors+1
  188. else
  189. success; echo
  190. rm -f $pidfile
  191. fi
  192. fi
  193. fi
  194. done
  195. if [ $errors -ge 1 ]; then
  196. echo -n "*** Error: $errors instance(s) unsuccessfully stopped"
  197. failure; echo
  198. else
  199. rm -f $lockfile
  200. fi
  201. }
  202. restart() {
  203. stop
  204. start
  205. }
  206. status() {
  207. for instance in $INSTANCES; do
  208. if [ -e $piddir/slapd-$instance.pid ]; then
  209. pid=$(cat $piddir/slapd-$instance.pid)
  210. if [ $(echo "$pids" | grep -c $pid) -ge 1 ]; then
  211. echo "$prog $instance (pid $pid) is running..."
  212. else
  213. echo "$prog $instance dead but pid file exists"
  214. fi
  215. else
  216. echo "$prog $instance is stopped"
  217. fi
  218. done
  219. }
  220. case "$1" in
  221. start|stop|restart|reload|status)
  222. $1
  223. ;;
  224. condrestart)
  225. [ ! -f $lockfile ] || restart
  226. ;;
  227. *)
  228. echo $"Usage: $0 {start|stop|status|restart|condrestart} [instance-name]"
  229. exit 2
  230. esac