1
0

MongoDB-init-Ubuntu 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/mongodb/$NAME.pid
  57. PIDDIR=`dirname $PIDFILE`
  58. ENABLE_MONGOD=yes
  59. # Include mongodb defaults if available.
  60. # All variables set before this point can be overridden by users, by
  61. # setting them directly in the defaults file. Use this to explicitly
  62. # override these values, at your own risk.
  63. if [ -f /etc/default/$NAME ] ; then
  64. . /etc/default/$NAME
  65. fi
  66. # Handle NUMA access to CPUs (SERVER-3574)
  67. # This verifies the existence of numactl as well as testing that the command works
  68. NUMACTL_ARGS="--interleave=all"
  69. if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  70. then
  71. NUMACTL="`which numactl` -- $NUMACTL_ARGS"
  72. DAEMON_OPTS=${DAEMON_OPTS:-"--config $CONF"}
  73. else
  74. NUMACTL=""
  75. DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $CONF"}
  76. fi
  77. if test ! -x $DAEMON; then
  78. echo "Could not find $DAEMON"
  79. exit 0
  80. fi
  81. if test "x$ENABLE_MONGOD" != "xyes"; then
  82. exit 0
  83. fi
  84. . /lib/lsb/init-functions
  85. STARTTIME=1
  86. DIETIME=10 # Time to wait for the server to die, in seconds
  87. # If this value is set too low you might not
  88. # let some servers to die gracefully and
  89. # 'restart' will not work
  90. DAEMONUSER=${DAEMONUSER:-mongod}
  91. DAEMONGROUP=${DAEMONGROUP:-mongod}
  92. set -e
  93. running_pid() {
  94. # Check if a given process pid's cmdline matches a given name
  95. pid=$1
  96. name=$2
  97. [ -z "$pid" ] && return 1
  98. [ ! -d /proc/$pid ] && return 1
  99. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  100. # Is this the expected server
  101. [ "$cmd" != "$name" ] && return 1
  102. return 0
  103. }
  104. running() {
  105. # Check if the process is running looking at /proc
  106. # (works for all users)
  107. # No pidfile, probably no daemon present
  108. [ ! -f "$PIDFILE" ] && return 1
  109. pid=`cat $PIDFILE`
  110. running_pid $pid $DAEMON || return 1
  111. return 0
  112. }
  113. start_server() {
  114. # Make sure the default pidfile directory exists
  115. if [ ! -d $PIDDIR ]; then
  116. install -d -m 0755 -o $DAEMONUSER -g $DAEMONGROUP $PIDDIR
  117. fi
  118. > $PIDFILE
  119. chown $DAEMONUSER.$DAEMONGROUP $PIDFILE
  120. # Recommended ulimit values for mongod or mongos
  121. # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
  122. #
  123. ulimit -f unlimited
  124. ulimit -t unlimited
  125. ulimit -v unlimited
  126. ulimit -n 64000
  127. ulimit -m unlimited
  128. ulimit -l unlimited
  129. # In dash, ulimit takes -p for maximum user processes
  130. # In bash, it's -u
  131. if readlink /proc/$$/exe | grep -q dash
  132. then
  133. ulimit -p 64000
  134. else
  135. ulimit -u 64000
  136. fi
  137. # Start the process using the wrapper
  138. start-stop-daemon --background --start --quiet --pidfile $PIDFILE \
  139. --make-pidfile --chuid $DAEMONUSER:$DAEMONGROUP \
  140. --exec $NUMACTL $DAEMON $DAEMON_OPTS
  141. errcode=$?
  142. return $errcode
  143. }
  144. stop_server() {
  145. # Stop the process using the wrapper
  146. start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  147. --retry 300 \
  148. --user $DAEMONUSER \
  149. --exec $DAEMON
  150. errcode=$?
  151. return $errcode
  152. }
  153. force_stop() {
  154. # Force the process to die killing it manually
  155. [ ! -e "$PIDFILE" ] && return
  156. if running ; then
  157. kill -15 $pid
  158. # Is it really dead?
  159. sleep "$DIETIME"s
  160. if running ; then
  161. kill -9 $pid
  162. sleep "$DIETIME"s
  163. if running ; then
  164. echo "Cannot kill $NAME (pid=$pid)!"
  165. exit 1
  166. fi
  167. fi
  168. fi
  169. rm -f $PIDFILE
  170. }
  171. case "$1" in
  172. start)
  173. log_daemon_msg "Starting $DESC" "$NAME"
  174. # Check if it's running first
  175. if running ; then
  176. log_progress_msg "apparently already running"
  177. log_end_msg 0
  178. exit 0
  179. fi
  180. if start_server ; then
  181. # NOTE: Some servers might die some time after they start,
  182. # this code will detect this issue if STARTTIME is set
  183. # to a reasonable value
  184. [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
  185. if running ; then
  186. # It's ok, the server started and is running
  187. log_end_msg 0
  188. else
  189. # It is not running after we did start
  190. log_end_msg 1
  191. fi
  192. else
  193. # Either we could not start it
  194. log_end_msg 1
  195. fi
  196. ;;
  197. stop)
  198. log_daemon_msg "Stopping $DESC" "$NAME"
  199. if running ; then
  200. # Only stop the server if we see it running
  201. errcode=0
  202. stop_server || errcode=$?
  203. log_end_msg $errcode
  204. else
  205. # If it's not running don't do anything
  206. log_progress_msg "apparently not running"
  207. log_end_msg 0
  208. exit 0
  209. fi
  210. ;;
  211. force-stop)
  212. # First try to stop gracefully the program
  213. $0 stop
  214. if running; then
  215. # If it's still running try to kill it more forcefully
  216. log_daemon_msg "Stopping (force) $DESC" "$NAME"
  217. errcode=0
  218. force_stop || errcode=$?
  219. log_end_msg $errcode
  220. fi
  221. ;;
  222. restart|force-reload)
  223. log_daemon_msg "Restarting $DESC" "$NAME"
  224. errcode=0
  225. stop_server || errcode=$?
  226. # Wait some sensible amount, some server need this
  227. [ -n "$DIETIME" ] && sleep $DIETIME
  228. start_server || errcode=$?
  229. [ -n "$STARTTIME" ] && sleep $STARTTIME
  230. running || errcode=$?
  231. log_end_msg $errcode
  232. ;;
  233. status)
  234. log_daemon_msg "Checking status of $DESC" "$NAME"
  235. if running ; then
  236. log_progress_msg "running"
  237. log_end_msg 0
  238. else
  239. log_progress_msg "apparently not running"
  240. log_end_msg 1
  241. exit 1
  242. fi
  243. ;;
  244. # MongoDB can't reload its configuration.
  245. reload)
  246. log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
  247. log_warning_msg "cannot re-read the config file (use restart)."
  248. ;;
  249. *)
  250. N=/etc/init.d/$NAME
  251. echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
  252. exit 1
  253. ;;
  254. esac
  255. exit 0