initscript.in 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #!/bin/sh
  2. #
  3. # @package_name@ This starts and stops @package_name@
  4. #
  5. # chkconfig: - 21 79
  6. # description: @capbrand@ 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. if [ -f /etc/rc.d/init.d/functions ] ; then
  14. . /etc/rc.d/init.d/functions
  15. fi
  16. # Source networking configuration.
  17. if [ -f /etc/sysconfig/network ] ; then
  18. . /etc/sysconfig/network
  19. fi
  20. # Check that networking is up.
  21. if [ "${NETWORKING}" = "no" ]
  22. then
  23. echo "Networking is down"
  24. exit 0
  25. fi
  26. # figure out which echo we're using
  27. ECHO_N=`echo -n`
  28. # some shells echo cannot use -n - linux echo by default cannot use \c
  29. echo_n()
  30. {
  31. if [ "$ECHO_N" = '-n' ] ; then
  32. echo "$*\c"
  33. else
  34. echo -n "$*"
  35. fi
  36. }
  37. # failure and success are not defined on some platforms
  38. type failure > /dev/null 2>&1 || {
  39. failure()
  40. {
  41. echo_n " FAILED"
  42. }
  43. }
  44. type success > /dev/null 2>&1 || {
  45. success()
  46. {
  47. echo_n " SUCCESS"
  48. }
  49. }
  50. # On Solaris /var/run is in tmpfs and gets wiped out upon reboot
  51. # we have to recreate the /var/run/@package_name@ directory
  52. # We also have to make sure that the directory is writable
  53. # by the directory server process
  54. # the argument to this function is the server instance directory,
  55. # which must have a dse.ldif file in it
  56. fix_pid_dir_ownership()
  57. {
  58. if [ ! -d $piddir ] ; then
  59. mkdir -p $piddir
  60. owner=`grep \^nsslapd-localuser $1/dse.ldif | awk '{print $2}'`
  61. if [ -n "$owner" ] ; then
  62. chown $owner $piddir
  63. chmod 700 $piddir
  64. fi
  65. fi
  66. }
  67. baseexec="ns-slapd"
  68. exec="@sbindir@/$baseexec"
  69. prog="@package_name@"
  70. # Lockfile
  71. if [ -d "@localstatedir@/lock/subsys" ] ; then
  72. lockfile="@localstatedir@/lock/subsys/@package_name@"
  73. else
  74. lockfile="@localstatedir@/lock/@package_name@/lock"
  75. fi
  76. # PID directory
  77. piddir="@localstatedir@/run/@package_name@"
  78. # Instance basedir
  79. instbase="@instconfigdir@"
  80. [ -f $exec ] || exit 0
  81. umask 077
  82. [ -f @initconfigdir@/@package_name@ ] && . @initconfigdir@/@package_name@
  83. INSTANCES=""
  84. for FILE in `/bin/ls -d $instbase/slapd-* 2>/dev/null`; do
  85. if [ -d "$FILE" ] ; then
  86. inst=`echo "$FILE" | sed -e "s|$instbase/slapd-||"`
  87. INSTANCES="$INSTANCES $inst"
  88. fi
  89. done
  90. if [ -n "$2" ]; then
  91. for I in $INSTANCES; do
  92. if [ "$2" = "$I" ]; then
  93. INSTANCES="$2"
  94. fi
  95. done
  96. if [ "$2" != "$INSTANCES" ]; then
  97. echo_n "$2 is an invalid @package_name@ instance"
  98. failure; echo
  99. exit 1
  100. fi
  101. fi
  102. start() {
  103. if [ -n "$INSTANCES" ]; then
  104. LD_LIBRARY_PATH=@libdir@/@package_name@:@nss_libdir@
  105. export LD_LIBRARY_PATH
  106. echo "Starting $prog: "
  107. # Start every slapd instance that isn't already running
  108. errors=0
  109. successes=0
  110. for instance in $INSTANCES; do
  111. echo_n " $instance..."
  112. # the server creates pidfile and writes the pid to it when it is fully
  113. # started and available to serve clients
  114. pidfile=$piddir/slapd-$instance.pid
  115. # the server creates startpidfile and writes the pid to just after
  116. # the process begins i.e. it received the startup request and didn't
  117. # die a horrible death (e.g. shared lib problem, oom, etc.)
  118. startpidfile=$piddir/slapd-$instance.startpid
  119. server_running=0
  120. if [ -f $pidfile ]; then
  121. pid=`cat $pidfile`
  122. if kill -0 $pid > /dev/null 2>&1 ; then
  123. echo_n " already running"
  124. success; echo
  125. successes=`expr $successes + 1`
  126. server_running=1
  127. else
  128. echo_n " not running, but pid file exists - attempt to start anyway..."
  129. rm -f $pidfile
  130. fi
  131. fi
  132. server_started=0
  133. if [ $server_running -eq 0 ] ; then
  134. rm -f $pidfile
  135. rm -f $startpidfile
  136. fix_pid_dir_ownership $instbase/slapd-$instance
  137. # start the directory server in a subshell so that the instance specific
  138. # init config environment will not apply to any other instance
  139. (
  140. [ -f @initconfigdir@/@package_name@-$instance ] && . @initconfigdir@/@package_name@-$instance
  141. $exec -D $instbase/slapd-$instance -i $pidfile -w $startpidfile
  142. )
  143. if [ $? -eq 0 ]; then
  144. server_started=1 # well, perhaps not running, but started ok
  145. else
  146. failure; echo
  147. errors=`expr $errors + 1`
  148. fi
  149. fi
  150. # ok, if we started the server successfully, let's see if it is really
  151. # running and ready to serve requests
  152. if [ $server_started -eq 1 ] ; then
  153. loop_counter=1
  154. # wait for 10 seconds for the start pid file to appear
  155. max_count=10
  156. while test $loop_counter -le $max_count; do
  157. loop_counter=`expr $loop_counter + 1`
  158. if test ! -f $startpidfile ; then
  159. sleep 1
  160. else
  161. pid=`cat $startpidfile`
  162. fi
  163. done
  164. if test ! -f $startpidfile ; then
  165. failure; echo
  166. errors=`expr $errors + 1`
  167. server_started=0
  168. fi
  169. fi
  170. # ok, server wrote the startpid file - let's see if it comes up
  171. # ready to service requests
  172. if [ $server_started -eq 1 ] ; then
  173. loop_counter=1
  174. # wait for 10 minutes (600 times 1 seconds)
  175. max_count=600
  176. while test $loop_counter -le $max_count ; do
  177. loop_counter=`expr $loop_counter + 1`
  178. if test ! -f $pidfile ; then
  179. if kill -0 $pid > /dev/null 2>&1 ; then
  180. sleep 1
  181. else
  182. break
  183. fi
  184. else
  185. pid=`cat $pidfile`
  186. break
  187. fi
  188. done
  189. if kill -0 $pid > /dev/null 2>&1 && test -f $pidfile ; then
  190. success; echo
  191. successes=`expr $successes + 1`
  192. else
  193. failure; echo
  194. errors=`expr $errors + 1`
  195. fi
  196. fi
  197. rm -f $startpidfile
  198. done
  199. if [ $successes -ge 1 ]; then
  200. touch $lockfile
  201. fi
  202. if [ $errors -ge 1 ]; then
  203. echo "*** Warning: $errors instance(s) failed to start"
  204. fi
  205. else
  206. echo "*** Error: no $prog instances configured"
  207. fi
  208. }
  209. stop() {
  210. echo "Shutting down $prog: "
  211. errors=0
  212. for instance in $INSTANCES; do
  213. pidfile=$piddir/slapd-$instance.pid
  214. if [ -f $pidfile ]; then
  215. pid=`cat $pidfile`
  216. echo_n " $instance..."
  217. server_stopped=0
  218. if kill -0 $pid > /dev/null 2>&1 ; then
  219. kill $pid
  220. if [ $? -eq 0 ]; then
  221. server_stopped=1
  222. else
  223. failure; echo
  224. errors=`expr $errors + 1`
  225. fi
  226. fi
  227. if [ $server_stopped -eq 1 ] ; then
  228. loop_counter=1
  229. # wait for 10 minutes (600 times 1 second)
  230. max_count=600
  231. while test $loop_counter -le $max_count; do
  232. loop_counter=`expr $loop_counter + 1`
  233. if kill -0 $pid > /dev/null 2>&1 ; then
  234. sleep 1
  235. else
  236. if test -f $pidfile ; then
  237. rm -f $pidfile
  238. fi
  239. break
  240. fi
  241. done
  242. if test -f $pidfile ; then
  243. failure; echo
  244. errors=`expr $errors + 1`
  245. else
  246. success; echo
  247. rm -f $pidfile
  248. fi
  249. fi
  250. fi
  251. done
  252. if [ $errors -ge 1 ]; then
  253. echo_n "*** Error: $errors instance(s) unsuccessfully stopped"
  254. failure; echo
  255. else
  256. rm -f $lockfile
  257. fi
  258. }
  259. restart() {
  260. stop
  261. start
  262. }
  263. status() {
  264. for instance in $INSTANCES; do
  265. if [ -f $piddir/slapd-$instance.pid ]; then
  266. pid=`cat $piddir/slapd-$instance.pid`
  267. if kill -0 $pid > /dev/null 2>&1 ; then
  268. echo "$prog $instance (pid $pid) is running..."
  269. else
  270. echo "$prog $instance dead but pid file exists"
  271. fi
  272. else
  273. echo "$prog $instance is stopped"
  274. fi
  275. done
  276. }
  277. case "$1" in
  278. start|stop|restart|reload|status)
  279. $1
  280. ;;
  281. condrestart)
  282. [ ! -f $lockfile ] || restart
  283. ;;
  284. *)
  285. echo Unknown command $1
  286. echo "Usage: $0 {start|stop|status|restart|condrestart} [instance-name]"
  287. exit 2
  288. esac