1
0

MongoDB-init-Ubuntu 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/sh
  2. #
  3. # init.d script with LSB support.
  4. #
  5. # Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
  6. #
  7. # This is free software; you may redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as
  9. # published by the Free Software Foundation; either version 2,
  10. # or (at your option) any later version.
  11. #
  12. # This is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License with
  18. # the Debian operating system, in /usr/share/common-licenses/GPL; if
  19. # not, write to the Free Software Foundation, Inc., 59 Temple Place,
  20. # Suite 330, Boston, MA 02111-1307 USA
  21. #
  22. ### BEGIN INIT INFO
  23. # Provides: mongod
  24. # Required-Start: $network $local_fs $remote_fs
  25. # Required-Stop: $network $local_fs $remote_fs
  26. # Should-Start: $named
  27. # Should-Stop:
  28. # Default-Start: 2 3 4 5
  29. # Default-Stop: 0 1 6
  30. # Short-Description: An object/document-oriented database
  31. # Description: MongoDB is a high-performance, open source, schema-free
  32. # document-oriented data store that's easy to deploy, manage
  33. # and use. It's network accessible, written in C++ and offers
  34. # the following features:
  35. #
  36. # * Collection oriented storage - easy storage of object-
  37. # style data
  38. # * Full index support, including on inner objects
  39. # * Query profiling
  40. # * Replication and fail-over support
  41. # * Efficient storage of binary data including large
  42. # objects (e.g. videos)
  43. # * Automatic partitioning for cloud-level scalability
  44. #
  45. # High performance, scalability, and reasonable depth of
  46. # functionality are the goals for the project.
  47. ### END INIT INFO
  48. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  49. DAEMON=/usr/local/mongodb/bin/mongod
  50. DESC=database
  51. NAME=mongod
  52. # Defaults. Can be overridden by the /etc/default/$NAME
  53. # Other configuration options are located in $CONF file. See here for more:
  54. # http://dochub.mongodb.org/core/configurationoptions
  55. CONF=/etc/mongod.conf
  56. PIDFILE=/var/run/$NAME.pid
  57. ENABLE_MONGOD=yes
  58. # Include mongodb defaults if available.
  59. # All variables set before this point can be overridden by users, by
  60. # setting them directly in the defaults file. Use this to explicitly
  61. # override these values, at your own risk.
  62. if [ -f /etc/default/$NAME ] ; then
  63. . /etc/default/$NAME
  64. fi
  65. # Handle NUMA access to CPUs (SERVER-3574)
  66. # This verifies the existence of numactl as well as testing that the command works
  67. NUMACTL_ARGS="--interleave=all"
  68. if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  69. then
  70. NUMACTL="`which numactl` -- $NUMACTL_ARGS"
  71. DAEMON_OPTS=${DAEMON_OPTS:-"--config $CONF"}
  72. else
  73. NUMACTL=""
  74. DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $CONF"}
  75. fi
  76. if test ! -x $DAEMON; then
  77. echo "Could not find $DAEMON"
  78. exit 0
  79. fi
  80. if test "x$ENABLE_MONGOD" != "xyes"; then
  81. exit 0
  82. fi
  83. . /lib/lsb/init-functions
  84. STARTTIME=1
  85. DIETIME=10 # Time to wait for the server to die, in seconds
  86. # If this value is set too low you might not
  87. # let some servers to die gracefully and
  88. # 'restart' will not work
  89. DAEMONUSER=${DAEMONUSER:-mongod}
  90. DAEMONGROUP=${DAEMONGROUP:-mongod}
  91. set -e
  92. running_pid() {
  93. # Check if a given process pid's cmdline matches a given name
  94. pid=$1
  95. name=$2
  96. [ -z "$pid" ] && return 1
  97. [ ! -d /proc/$pid ] && return 1
  98. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  99. # Is this the expected server
  100. [ "$cmd" != "$name" ] && return 1
  101. return 0
  102. }
  103. running() {
  104. # Check if the process is running looking at /proc
  105. # (works for all users)
  106. # No pidfile, probably no daemon present
  107. [ ! -f "$PIDFILE" ] && return 1
  108. pid=`cat $PIDFILE`
  109. running_pid $pid $DAEMON || return 1
  110. return 0
  111. }
  112. start_server() {
  113. # Recommended ulimit values for mongod or mongos
  114. # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
  115. #
  116. ulimit -f unlimited
  117. ulimit -t unlimited
  118. ulimit -v unlimited
  119. ulimit -n 64000
  120. ulimit -m unlimited
  121. ulimit -l unlimited
  122. # In dash, ulimit takes -p for maximum user processes
  123. # In bash, it's -u
  124. if readlink /proc/$$/exe | grep -q dash
  125. then
  126. ulimit -p 64000
  127. else
  128. ulimit -u 64000
  129. fi
  130. # Start the process using the wrapper
  131. start-stop-daemon --background --start --quiet --pidfile $PIDFILE \
  132. --make-pidfile --chuid $DAEMONUSER:$DAEMONGROUP \
  133. --exec $NUMACTL $DAEMON $DAEMON_OPTS
  134. errcode=$?
  135. return $errcode
  136. }
  137. stop_server() {
  138. # Stop the process using the wrapper
  139. start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  140. --retry 300 \
  141. --user $DAEMONUSER \
  142. --exec $DAEMON
  143. errcode=$?
  144. return $errcode
  145. }
  146. force_stop() {
  147. # Force the process to die killing it manually
  148. [ ! -e "$PIDFILE" ] && return
  149. if running ; then
  150. kill -15 $pid
  151. # Is it really dead?
  152. sleep "$DIETIME"s
  153. if running ; then
  154. kill -9 $pid
  155. sleep "$DIETIME"s
  156. if running ; then
  157. echo "Cannot kill $NAME (pid=$pid)!"
  158. exit 1
  159. fi
  160. fi
  161. fi
  162. rm -f $PIDFILE
  163. }
  164. case "$1" in
  165. start)
  166. log_daemon_msg "Starting $DESC" "$NAME"
  167. # Check if it's running first
  168. if running ; then
  169. log_progress_msg "apparently already running"
  170. log_end_msg 0
  171. exit 0
  172. fi
  173. if start_server ; then
  174. # NOTE: Some servers might die some time after they start,
  175. # this code will detect this issue if STARTTIME is set
  176. # to a reasonable value
  177. [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
  178. if running ; then
  179. # It's ok, the server started and is running
  180. log_end_msg 0
  181. else
  182. # It is not running after we did start
  183. log_end_msg 1
  184. fi
  185. else
  186. # Either we could not start it
  187. log_end_msg 1
  188. fi
  189. ;;
  190. stop)
  191. log_daemon_msg "Stopping $DESC" "$NAME"
  192. if running ; then
  193. # Only stop the server if we see it running
  194. errcode=0
  195. stop_server || errcode=$?
  196. log_end_msg $errcode
  197. else
  198. # If it's not running don't do anything
  199. log_progress_msg "apparently not running"
  200. log_end_msg 0
  201. exit 0
  202. fi
  203. ;;
  204. force-stop)
  205. # First try to stop gracefully the program
  206. $0 stop
  207. if running; then
  208. # If it's still running try to kill it more forcefully
  209. log_daemon_msg "Stopping (force) $DESC" "$NAME"
  210. errcode=0
  211. force_stop || errcode=$?
  212. log_end_msg $errcode
  213. fi
  214. ;;
  215. restart|force-reload)
  216. log_daemon_msg "Restarting $DESC" "$NAME"
  217. errcode=0
  218. stop_server || errcode=$?
  219. # Wait some sensible amount, some server need this
  220. [ -n "$DIETIME" ] && sleep $DIETIME
  221. start_server || errcode=$?
  222. [ -n "$STARTTIME" ] && sleep $STARTTIME
  223. running || errcode=$?
  224. log_end_msg $errcode
  225. ;;
  226. status)
  227. log_daemon_msg "Checking status of $DESC" "$NAME"
  228. if running ; then
  229. log_progress_msg "running"
  230. log_end_msg 0
  231. else
  232. log_progress_msg "apparently not running"
  233. log_end_msg 1
  234. exit 1
  235. fi
  236. ;;
  237. # MongoDB can't reload its configuration.
  238. reload)
  239. log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
  240. log_warning_msg "cannot re-read the config file (use restart)."
  241. ;;
  242. *)
  243. N=/etc/init.d/$NAME
  244. echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
  245. exit 1
  246. ;;
  247. esac
  248. exit 0