Posted By

javierav on 08/10/10


Tagged

mongodb


Versions (?)

mongodb init.d script


 / Published in: Bash
 

  1. #!/bin/sh
  2. #
  3. # init.d script with LSB support.
  4. #
  5. # Copyright (c) 2007 Javier Fernandez-Sanguino <[email protected]>
  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: mongodb
  24. # Required-Start: $network $local_fs
  25. # Required-Stop:
  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. # * Auto-sharding for cloud-level scalability (Q209)
  44. #
  45.  
  46. # High performance, scalability, and reasonable depth of
  47. # functionality are the goals for the project.
  48. ### END INIT INFO
  49.  
  50. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  51. DAEMON=/usr/local/bin/mongod
  52. DAEMON_OPTS='-f /etc/mongodb/mongodb.conf run'
  53. NAME=MongoDB
  54. DESC=database
  55.  
  56. test -x $DAEMON || exit 0
  57.  
  58.  
  59. . /lib/lsb/init-functions
  60.  
  61. LOGDIR=/var/log/mongodb
  62. PIDFILE=/var/run/$NAME.pid
  63. DIETIME=10 # Time to wait for the server to die, in seconds
  64. # If this value is set too low you might not
  65. # let some servers to die gracefully and
  66. # 'restart' will not work
  67.  
  68. LOGFILE=$LOGDIR/$NAME.log # Server logfile
  69.  
  70.  
  71. # Include mongodb defaults if available
  72. if [ -f /etc/default/$NAME ] ; then
  73. . /etc/default/$NAME
  74. fi
  75.  
  76. DAEMONUSER=mongodb
  77. # Check that the user exists (if we set a user)
  78. # Does the user exist?
  79. if [ -n "$DAEMONUSER" ] ; then
  80. if getent passwd | grep -q "^$DAEMONUSER:"; then
  81. # Obtain the uid and gid
  82. DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
  83. DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
  84. else
  85. log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
  86. exit 1
  87. fi
  88.  
  89. fi
  90.  
  91. set -e
  92.  
  93.  
  94. running_pid() {
  95. # Check if a given process pid's cmdline matches a given name
  96. pid=$1
  97. name=$2
  98. [ -z "$pid" ] && return 1
  99. [ ! -d /proc/$pid ] && return 1
  100. cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
  101. # Is this the expected server
  102. [ "$cmd" != "$name" ] && return 1
  103. return 0
  104. }
  105.  
  106. running() {
  107. # Check if the process is running looking at /proc
  108. # (works for all users)
  109.  
  110. # No pidfile, probably no daemon present
  111. [ ! -f "$PIDFILE" ] && return 1
  112. pid=`cat $PIDFILE`
  113. running_pid $pid $DAEMON || return 1
  114. return 0
  115. }
  116.  
  117. start_server() {
  118. # Start the process using the wrapper
  119. if [ -z "$DAEMONUSER" ] ; then
  120. start-stop-daemon --background --start --quiet --pidfile $PIDFILE \
  121. --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
  122. errcode=$?
  123. else
  124. echo $DAEMONUSER
  125.  
  126. # if we are using a daemonuser then change the user id
  127. start-stop-daemon --background --start --quiet --pidfile $PIDFILE \
  128. --make-pidfile --chuid $DAEMONUSER \
  129. --exec $DAEMON -- $DAEMON_OPTS
  130. errcode=$?
  131.  
  132. fi
  133. return $errcode
  134. }
  135.  
  136. stop_server() {
  137. # Stop the process using the wrapper
  138. if [ -z "$DAEMONUSER" ] ; then
  139. start-stop-daemon --stop --quiet --pidfile $PIDFILE
  140. rm $PIDFILE
  141. errcode=$?
  142. else
  143. # if we are using a daemonuser then look for process that match
  144. start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  145. --user $DAEMONUSER \
  146. --exec $DAEMON
  147. errcode=$?
  148. fi
  149.  
  150. return $errcode
  151. }
  152. reload_server() {
  153. [ ! -f "$PIDFILE" ] && return 1
  154. pid=pidofproc $PIDFILE # This is the daemon's pid
  155. # Send a SIGHUP
  156. kill -1 $pid
  157. return $?
  158. }
  159.  
  160. force_stop() {
  161. # Force the process to die killing it manually
  162. [ ! -e "$PIDFILE" ] && return
  163. if running ; then
  164. kill -15 $pid
  165. # Is it really dead?
  166. sleep "$DIETIME"s
  167. if running ; then
  168. kill -9 $pid
  169. sleep "$DIETIME"s
  170. if running ; then
  171. echo "Cannot kill $NAME (pid=$pid)!"
  172. exit 1
  173. fi
  174. fi
  175. fi
  176. rm -f $PIDFILE
  177. }
  178.  
  179.  
  180. case "$1" in
  181. start)
  182. log_daemon_msg "Starting $DESC $NAME"
  183. # Check if it's running first
  184. if running ; then
  185. log_progress_msg "apparently already running"
  186. log_end_msg 0
  187. exit 0
  188. fi
  189. if start_server ; then
  190. # NOTE: Some servers might die some time after they start,
  191. # this code will detect this issue if STARTTIME is set
  192. # to a reasonable value
  193. [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
  194.  
  195. if running ; then
  196. # It's ok, the server started and is running
  197. log_end_msg 0
  198. else
  199. # It is not running after we did start
  200. log_end_msg 1
  201. fi
  202. else
  203. # Either we could not start it
  204. log_end_msg 1
  205. fi
  206. ;;
  207. stop)
  208. log_daemon_msg "Stopping $DESC" "$NAME"
  209. if running ; then
  210. # Only stop the server if we see it running
  211. errcode=0
  212. stop_server || errcode=$?
  213. log_end_msg $errcode
  214. else
  215. # If it's not running don't do anything
  216.  
  217. log_progress_msg "apparently not running"
  218. log_end_msg 0
  219. exit 0
  220. fi
  221. ;;
  222. force-stop)
  223. # First try to stop gracefully the program
  224. $0 stop
  225. if running; then
  226. # If it's still running try to kill it more forcefully
  227. log_daemon_msg "Stopping (force) $DESC" "$NAME"
  228. errcode=0
  229. force_stop || errcode=$?
  230. log_end_msg $errcode
  231. fi
  232. ;;
  233. restart|force-reload)
  234. log_daemon_msg "Restarting $DESC" "$NAME"
  235. errcode=0
  236. stop_server || errcode=$?
  237. # Wait some sensible amount, some server need this
  238.  
  239. [ -n "$DIETIME" ] && sleep $DIETIME
  240. start_server || errcode=$?
  241. [ -n "$STARTTIME" ] && sleep $STARTTIME
  242. running || errcode=$?
  243. log_end_msg $errcode
  244. ;;
  245. status)
  246.  
  247. log_daemon_msg "Checking status of $DESC" "$NAME"
  248. if running ; then
  249. log_progress_msg "running"
  250. log_end_msg 0
  251. else
  252. log_progress_msg "apparently not running"
  253. log_end_msg 1
  254. exit 1
  255. fi
  256. ;;
  257. # Use this if the daemon cannot reload
  258. reload)
  259. log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
  260.  
  261. log_warning_msg "cannot re-read the config file (use restart)."
  262. ;;
  263. # And this if it cann
  264. #reload)
  265. #
  266. # If the daemon can reload its config files on the fly
  267. # for example by sending it SIGHUP, do it here.
  268. #
  269. # If the daemon responds to changes in its config file
  270. # directly anyway, make this a do-nothing entry.
  271. #
  272. # log_daemon_msg "Reloading $DESC configuration files" "$NAME"
  273. # if running ; then
  274. # reload_server
  275. # if ! running ; then
  276. # Process died after we tried to reload
  277. # log_progress_msg "died on reload"
  278. # log_end_msg 1
  279. # exit 1
  280. # fi
  281. # else
  282.  
  283. # log_progress_msg "server is not running"
  284. # log_end_msg 1
  285. # exit 1
  286. # fi
  287. #;;
  288.  
  289. *)
  290. N=/etc/init.d/$NAME
  291. echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
  292. exit 1
  293. ;;
  294. esac
  295.  
  296. exit 0

Report this snippet  

You need to login to post a comment.