init.d.ex 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #! /bin/sh
  2. #
  3. # skeleton example file to build /etc/init.d/ scripts.
  4. # This file should be used to construct scripts for /etc/init.d.
  5. #
  6. # Written by Miquel van Smoorenburg <miquels@cistron.nl>.
  7. # Modified for Debian
  8. # by Ian Murdock <imurdock@gnu.ai.mit.edu>.
  9. # Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
  10. #
  11. # Version: @(#)skeleton 1.9 26-Feb-2001 miquels@cistron.nl
  12. #
  13. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  14. DAEMON=/usr/sbin/cvm
  15. NAME=cvm
  16. DESC=cvm
  17. test -x $DAEMON || exit 0
  18. LOGDIR=/var/log/cvm
  19. PIDFILE=/var/run/$NAME.pid
  20. DODTIME=1 # Time to wait for the server to die, in seconds
  21. # If this value is set too low you might not
  22. # let some servers to die gracefully and
  23. # 'restart' will not work
  24. # Include cvm defaults if available
  25. if [ -f /etc/default/cvm ] ; then
  26. . /etc/default/cvm
  27. fi
  28. set -e
  29. running_pid()
  30. {
  31. # Check if a given process pid's cmdline matches a given name
  32. pid=$1
  33. name=$2
  34. [ -z "$pid" ] && return 1
  35. [ ! -d /proc/$pid ] && return 1
  36. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  37. # Is this the expected child?
  38. [ "$cmd" != "$name" ] && return 1
  39. return 0
  40. }
  41. running()
  42. {
  43. # Check if the process is running looking at /proc
  44. # (works for all users)
  45. # No pidfile, probably no daemon present
  46. [ ! -f "$PIDFILE" ] && return 1
  47. # Obtain the pid and check it against the binary name
  48. pid=`cat $PIDFILE`
  49. running_pid $pid $DAEMON || return 1
  50. return 0
  51. }
  52. force_stop() {
  53. # Forcefully kill the process
  54. [ ! -f "$PIDFILE" ] && return
  55. if running ; then
  56. kill -15 $pid
  57. # Is it really dead?
  58. [ -n "$DODTIME" ] && sleep "$DODTIME"s
  59. if running ; then
  60. kill -9 $pid
  61. [ -n "$DODTIME" ] && sleep "$DODTIME"s
  62. if running ; then
  63. echo "Cannot kill $LABEL (pid=$pid)!"
  64. exit 1
  65. fi
  66. fi
  67. fi
  68. rm -f $PIDFILE
  69. return 0
  70. }
  71. case "$1" in
  72. start)
  73. echo -n "Starting $DESC: "
  74. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  75. --exec $DAEMON -- $DAEMON_OPTS
  76. if running ; then
  77. echo "$NAME."
  78. else
  79. echo " ERROR."
  80. fi
  81. ;;
  82. stop)
  83. echo -n "Stopping $DESC: "
  84. start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  85. --exec $DAEMON
  86. echo "$NAME."
  87. ;;
  88. force-stop)
  89. echo -n "Forcefully stopping $DESC: "
  90. force_stop
  91. if ! running ; then
  92. echo "$NAME."
  93. else
  94. echo " ERROR."
  95. fi
  96. ;;
  97. #reload)
  98. #
  99. # If the daemon can reload its config files on the fly
  100. # for example by sending it SIGHUP, do it here.
  101. #
  102. # If the daemon responds to changes in its config file
  103. # directly anyway, make this a do-nothing entry.
  104. #
  105. # echo "Reloading $DESC configuration files."
  106. # start-stop-daemon --stop --signal 1 --quiet --pidfile \
  107. # /var/run/$NAME.pid --exec $DAEMON
  108. #;;
  109. force-reload)
  110. #
  111. # If the "reload" option is implemented, move the "force-reload"
  112. # option to the "reload" entry above. If not, "force-reload" is
  113. # just the same as "restart" except that it does nothing if the
  114. # daemon isn't already running.
  115. # check wether $DAEMON is running. If so, restart
  116. start-stop-daemon --stop --test --quiet --pidfile \
  117. /var/run/$NAME.pid --exec $DAEMON \
  118. && $0 restart \
  119. || exit 0
  120. ;;
  121. restart)
  122. echo -n "Restarting $DESC: "
  123. start-stop-daemon --stop --quiet --pidfile \
  124. /var/run/$NAME.pid --exec $DAEMON
  125. [ -n "$DODTIME" ] && sleep $DODTIME
  126. start-stop-daemon --start --quiet --pidfile \
  127. /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
  128. echo "$NAME."
  129. ;;
  130. status)
  131. echo -n "$LABEL is "
  132. if running ; then
  133. echo "running"
  134. else
  135. echo " not running."
  136. exit 1
  137. fi
  138. ;;
  139. *)
  140. N=/etc/init.d/$NAME
  141. # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
  142. echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
  143. exit 1
  144. ;;
  145. esac
  146. exit 0