init.d.lsb.ex 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/bin/sh
  2. #
  3. # Example init.d script with LSB support.
  4. #
  5. # Please read this init.d carefully and modify the sections to
  6. # adjust it to the program you want to run.
  7. #
  8. # Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
  9. #
  10. # This is free software; you may redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as
  12. # published by the Free Software Foundation; either version 2,
  13. # or (at your option) any later version.
  14. #
  15. # This is distributed in the hope that it will be useful, but
  16. # WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License with
  21. # the Debian operating system, in /usr/share/common-licenses/GPL; if
  22. # not, write to the Free Software Foundation, Inc., 59 Temple Place,
  23. # Suite 330, Boston, MA 02111-1307 USA
  24. #
  25. ### BEGIN INIT INFO
  26. # Provides: cvm
  27. # Required-Start: $network $local_fs
  28. # Required-Stop:
  29. # Should-Start: $named
  30. # Should-Stop:
  31. # Default-Start: 2 3 4 5
  32. # Default-Stop: 0 1 6
  33. # Short-Description: <Enter a short description of the sortware>
  34. # Description: <Enter a long description of the software>
  35. # <...>
  36. # <...>
  37. ### END INIT INFO
  38. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  39. DAEMON=/usr/sbin/cvm # Introduce the server's location here
  40. NAME=#PACKAGE # Introduce the short server's name here
  41. DESC=#PACKAGE # Introduce a short description here
  42. LOGDIR=/var/log/cvm # Log directory to use
  43. PIDFILE=/var/run/$NAME.pid
  44. test -x $DAEMON || exit 0
  45. . /lib/lsb/init-functions
  46. # Default options, these can be overriden by the information
  47. # at /etc/default/$NAME
  48. DAEMON_OPTS="" # Additional options given to the server
  49. DIETIME=10 # Time to wait for the server to die, in seconds
  50. # If this value is set too low you might not
  51. # let some servers to die gracefully and
  52. # 'restart' will not work
  53. #STARTTIME=2 # Time to wait for the server to start, in seconds
  54. # If this value is set each time the server is
  55. # started (on start or restart) the script will
  56. # stall to try to determine if it is running
  57. # If it is not set and the server takes time
  58. # to setup a pid file the log message might
  59. # be a false positive (says it did not start
  60. # when it actually did)
  61. LOGFILE=$LOGDIR/$NAME.log # Server logfile
  62. #DAEMONUSER=cvm # Users to run the daemons as. If this value
  63. # is set start-stop-daemon will chuid the server
  64. # Include defaults if available
  65. if [ -f /etc/default/$NAME ] ; then
  66. . /etc/default/$NAME
  67. fi
  68. # Use this if you want the user to explicitly set 'RUN' in
  69. # /etc/default/
  70. #if [ "x$RUN" != "xyes" ] ; then
  71. # log_failure_msg "$NAME disabled, please adjust the configuration to your needs "
  72. # log_failure_msg "and then set RUN to 'yes' in /etc/default/$NAME to enable it."
  73. # exit 1
  74. #fi
  75. # Check that the user exists (if we set a user)
  76. # Does the user exist?
  77. if [ -n "$DAEMONUSER" ] ; then
  78. if getent passwd | grep -q "^$DAEMONUSER:"; then
  79. # Obtain the uid and gid
  80. DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
  81. DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
  82. else
  83. log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
  84. exit 1
  85. fi
  86. fi
  87. set -e
  88. running_pid() {
  89. # Check if a given process pid's cmdline matches a given name
  90. pid=$1
  91. name=$2
  92. [ -z "$pid" ] && return 1
  93. [ ! -d /proc/$pid ] && return 1
  94. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  95. # Is this the expected server
  96. [ "$cmd" != "$name" ] && return 1
  97. return 0
  98. }
  99. running() {
  100. # Check if the process is running looking at /proc
  101. # (works for all users)
  102. # No pidfile, probably no daemon present
  103. [ ! -f "$PIDFILE" ] && return 1
  104. pid=`cat $PIDFILE`
  105. running_pid $pid $DAEMON || return 1
  106. return 0
  107. }
  108. start_server() {
  109. # Start the process using the wrapper
  110. if [ -z "$DAEMONUSER" ] ; then
  111. start_daemon -p $PIDFILE $DAEMON -- $DAEMON_OPTS
  112. errcode=$?
  113. else
  114. # if we are using a daemonuser then change the user id
  115. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  116. --chuid $DAEMONUSER \
  117. --exec $DAEMON -- $DAEMON_OPTS
  118. errcode=$?
  119. fi
  120. return $errcode
  121. }
  122. stop_server() {
  123. # Stop the process using the wrapper
  124. if [ -z "$DAEMONUSER" ] ; then
  125. killproc -p $PIDFILE $DAEMON
  126. errcode=$?
  127. else
  128. # if we are using a daemonuser then look for process that match
  129. start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  130. --user $DAEMONUSER \
  131. --exec $DAEMON
  132. errcode=$?
  133. fi
  134. return $errcode
  135. }
  136. reload_server() {
  137. [ ! -f "$PIDFILE" ] && return 1
  138. pid=pidofproc $PIDFILE # This is the daemon's pid
  139. # Send a SIGHUP
  140. kill -1 $pid
  141. return $?
  142. }
  143. force_stop() {
  144. # Force the process to die killing it manually
  145. [ ! -e "$PIDFILE" ] && return
  146. if running ; then
  147. kill -15 $pid
  148. # Is it really dead?
  149. sleep "$DIETIME"s
  150. if running ; then
  151. kill -9 $pid
  152. sleep "$DIETIME"s
  153. if running ; then
  154. echo "Cannot kill $NAME (pid=$pid)!"
  155. exit 1
  156. fi
  157. fi
  158. fi
  159. rm -f $PIDFILE
  160. }
  161. case "$1" in
  162. start)
  163. log_daemon_msg "Starting $DESC " "$NAME"
  164. # Check if it's running first
  165. if running ; then
  166. log_progress_msg "apparently already running"
  167. log_end_msg 0
  168. exit 0
  169. fi
  170. if start_server ; then
  171. # NOTE: Some servers might die some time after they start,
  172. # this code will detect this issue if STARTTIME is set
  173. # to a reasonable value
  174. [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
  175. if running ; then
  176. # It's ok, the server started and is running
  177. log_end_msg 0
  178. else
  179. # It is not running after we did start
  180. log_end_msg 1
  181. fi
  182. else
  183. # Either we could not start it
  184. log_end_msg 1
  185. fi
  186. ;;
  187. stop)
  188. log_daemon_msg "Stopping $DESC" "$NAME"
  189. if running ; then
  190. # Only stop the server if we see it running
  191. errcode=0
  192. stop_server || errcode=$?
  193. log_end_msg $errcode
  194. else
  195. # If it's not running don't do anything
  196. log_progress_msg "apparently not running"
  197. log_end_msg 0
  198. exit 0
  199. fi
  200. ;;
  201. force-stop)
  202. # First try to stop gracefully the program
  203. $0 stop
  204. if running; then
  205. # If it's still running try to kill it more forcefully
  206. log_daemon_msg "Stopping (force) $DESC" "$NAME"
  207. errcode=0
  208. force_stop || errcode=$?
  209. log_end_msg $errcode
  210. fi
  211. ;;
  212. restart|force-reload)
  213. log_daemon_msg "Restarting $DESC" "$NAME"
  214. errcode=0
  215. stop_server || errcode=$?
  216. # Wait some sensible amount, some server need this
  217. [ -n "$DIETIME" ] && sleep $DIETIME
  218. start_server || errcode=$?
  219. [ -n "$STARTTIME" ] && sleep $STARTTIME
  220. running || errcode=$?
  221. log_end_msg $errcode
  222. ;;
  223. status)
  224. log_daemon_msg "Checking status of $DESC" "$NAME"
  225. if running ; then
  226. log_progress_msg "running"
  227. log_end_msg 0
  228. else
  229. log_progress_msg "apparently not running"
  230. log_end_msg 1
  231. exit 1
  232. fi
  233. ;;
  234. # Use this if the daemon cannot reload
  235. reload)
  236. log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
  237. log_warning_msg "cannot re-read the config file (use restart)."
  238. ;;
  239. # And this if it cann
  240. #reload)
  241. #
  242. # If the daemon can reload its config files on the fly
  243. # for example by sending it SIGHUP, do it here.
  244. #
  245. # If the daemon responds to changes in its config file
  246. # directly anyway, make this a do-nothing entry.
  247. #
  248. # log_daemon_msg "Reloading $DESC configuration files" "$NAME"
  249. # if running ; then
  250. # reload_server
  251. # if ! running ; then
  252. # Process died after we tried to reload
  253. # log_progress_msg "died on reload"
  254. # log_end_msg 1
  255. # exit 1
  256. # fi
  257. # else
  258. # log_progress_msg "server is not running"
  259. # log_end_msg 1
  260. # exit 1
  261. # fi
  262. #;;
  263. *)
  264. N=/etc/init.d/$NAME
  265. echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
  266. exit 1
  267. ;;
  268. esac
  269. exit 0