init-d-script 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/bin/sh
  2. # See init-d-script(5) for instructions on how to use this library.
  3. #=============================================================================
  4. # Shift arguments early to fix #826214
  5. __init_d_script_name="$1"
  6. shift
  7. # Define LSB log_* functions.
  8. # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
  9. # and status_of_proc is working.
  10. . /lib/lsb/init-functions
  11. # PATH should only include /usr/* if it runs after the mountnfs.sh
  12. # script. Scripts running before mountnfs.sh should remove the /usr/*
  13. # entries.
  14. PATH=/usr/sbin:/usr/bin:/sbin:/bin
  15. export PATH
  16. is_call_implemented() {
  17. command -V $1 > /dev/null 2>&1
  18. }
  19. do_usage() {
  20. if is_call_implemented do_reload ; then
  21. echo "Usage: $SCRIPTNAME {start|stop|status|reload|restart|try-restart|force-reload}" >&2
  22. else
  23. echo "Usage: $SCRIPTNAME {start|stop|status|restart|try-restart|force-reload}" >&2
  24. fi
  25. }
  26. call() {
  27. cmd="$1"
  28. shift
  29. if is_call_implemented ${cmd}_override ; then
  30. ${cmd}_override "$@"
  31. else
  32. ${cmd} "$@"
  33. fi
  34. }
  35. #
  36. # Function that starts the daemon/service
  37. #
  38. do_start_cmd() {
  39. start-stop-daemon --start --quiet --oknodo \
  40. ${PIDFILE:+--pidfile ${PIDFILE}} $START_ARGS \
  41. --startas $DAEMON --name ${COMMAND_NAME} --exec $DAEMON -- $DAEMON_ARGS
  42. }
  43. do_start()
  44. {
  45. if is_call_implemented do_start_prepare ; then
  46. call do_start_prepare
  47. fi
  48. log_daemon_msg "Starting $DESC" "$NAME"
  49. call do_start_cmd
  50. retval=$?
  51. case ${retval} in
  52. 0|1) vlog_end_msg 0 ;;
  53. 2) vlog_end_msg 1 ;;
  54. esac
  55. if is_call_implemented do_start_cleanup ; then
  56. call do_start_cleanup
  57. else
  58. return ${retval}
  59. fi
  60. }
  61. #
  62. # Function that stops the daemon/service
  63. #
  64. do_stop_cmd() {
  65. start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
  66. $STOP_ARGS \
  67. ${PIDFILE:+--pidfile ${PIDFILE}} --name ${COMMAND_NAME} --exec $DAEMON
  68. RETVAL="$?"
  69. [ "$RETVAL" = 2 ] && return 2
  70. # Wait for children to finish too if this is a daemon that forks
  71. # and if the daemon is only ever run from this initscript.
  72. # If the above conditions are not satisfied then add some other code
  73. # that waits for the process to drop all resources that could be
  74. # needed by services started subsequently. A last resort is to
  75. # sleep for some time.
  76. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
  77. $STOP_ARGS \
  78. --exec $DAEMON
  79. [ "$?" = 2 ] && return 2
  80. # Many daemons don't delete their pidfiles when they exit.
  81. rm -f $PIDFILE
  82. return $RETVAL
  83. }
  84. do_stop()
  85. {
  86. if is_call_implemented do_stop_prepare ; then
  87. call do_stop_prepare
  88. fi
  89. vlog_daemon_msg "Stopping $DESC" "$NAME"
  90. call do_stop_cmd
  91. retval=$?
  92. case ${retval} in
  93. 0|1) vlog_end_msg 0 ;;
  94. 2) vlog_end_msg 1 ;;
  95. esac
  96. if is_call_implemented do_stop_cleanup ; then
  97. call do_stop_cleanup
  98. else
  99. return ${retval}
  100. fi
  101. }
  102. do_restart() {
  103. if is_call_implemented do_restart_prepare ; then
  104. call do_restart_prepare
  105. fi
  106. vlog_daemon_msg "Restarting $DESC" "$NAME"
  107. call do_stop_cmd
  108. call do_start_cmd
  109. retval=$?
  110. case ${retval} in
  111. 0|1) vlog_end_msg 0 ;;
  112. 2) vlog_end_msg 1 ;;
  113. esac
  114. if is_call_implemented do_restart_cleanup ; then
  115. call do_restart_cleanup
  116. else
  117. return ${retval}
  118. fi
  119. }
  120. do_force_reload() {
  121. if is_call_implemented do_reload ; then
  122. call do_reload
  123. else
  124. call do_restart
  125. fi
  126. }
  127. # Enable this using
  128. # alias do_reload=do_reload_sigusr1
  129. do_reload_sigusr1() {
  130. if is_call_implemented do_reload_prepare ; then
  131. call do_reload_prepare
  132. fi
  133. log_daemon_msg "Reloading $DESC configuration files" "$NAME"
  134. start-stop-daemon --oknodo --stop --signal 1 --quiet \
  135. --pidfile "$PIDFILE" --exec "$DAEMON"
  136. log_end_msg $?
  137. if is_call_implemented do_reload_cleanup ; then
  138. call do_reload_cleanup
  139. fi
  140. }
  141. do_status() {
  142. # FIXME: Does it make sense to call `status_of_proc' if PIDFILE is
  143. # empty?
  144. status_of_proc "$DAEMON" "$NAME" ${PIDFILE:="-p ${PIDFILE}"}
  145. }
  146. if [ "$DEBUG" = "true" ] ; then
  147. set -x
  148. fi
  149. # Unset configuration variables to make sure that if variable is not assigned a
  150. # value in init script, it does not use one from environment. See #822918.
  151. unset DAEMON DAEMON_ARGS NAME COMMAND_NAME PIDFILE
  152. SCRIPTNAME="$__init_d_script_name"
  153. scriptbasename="$(basename "$__init_d_script_name")"
  154. if [ "$scriptbasename" != "init-d-script" ] ; then
  155. . "$__init_d_script_name"
  156. else
  157. exit 0
  158. fi
  159. NAME=${NAME:=$(basename $DAEMON)}
  160. DESC=${DESC:=$NAME}
  161. : ${COMMAND_NAME:=${NAME}}
  162. # Do not use pid file if $PIDFILE is 'none'. Otherwise, generate from
  163. # $NAME or use the value provided by the init.d script.
  164. if [ none = "$PIDFILE" ] ; then
  165. PIDFILE=
  166. elif [ -z "$PIDFILE" ] ; then
  167. PIDFILE=/var/run/$NAME.pid
  168. fi
  169. # Read configuration variable file if it is present
  170. [ -r "/etc/default/${NAME}" ] && . "/etc/default/${NAME}"
  171. # Exit if the package is not installed
  172. if [ none != "$DAEMON" ] && [ ! -x "$DAEMON" ] ; then
  173. exit 0
  174. fi
  175. # Load the VERBOSE setting and other rcS variables
  176. . /lib/init/vars.sh
  177. if [ -t 0 ] ; then # Be verbose when called from a terminal
  178. VERBOSE=yes
  179. fi
  180. case "$1" in
  181. start)
  182. call do_start
  183. ;;
  184. stop)
  185. call do_stop
  186. ;;
  187. status)
  188. call do_status
  189. ;;
  190. reload)
  191. if is_call_implemented do_reload ; then
  192. do_reload
  193. else
  194. call do_usage
  195. exit 3
  196. fi
  197. ;;
  198. force-reload)
  199. call do_force_reload
  200. ;;
  201. restart)
  202. call do_restart
  203. ;;
  204. try-restart)
  205. log_daemon_msg "Trying to restart $DESC" "$NAME"
  206. if call do_status > /dev/null 2>&1 ; then
  207. call do_restart
  208. log_end_msg $?
  209. else
  210. log_progress_msg "is not running."
  211. log_end_msg 1
  212. fi
  213. ;;
  214. '')
  215. call do_usage
  216. exit 3
  217. ;;
  218. *)
  219. if is_call_implemented do_unknown ; then
  220. call do_unknown "$1"
  221. exit 3
  222. else
  223. call do_usage
  224. exit 3
  225. fi
  226. ;;
  227. esac
  228. exit $? # See https://bugs.debian.org/822753#53