MongoDB-init-CentOS 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. # mongod - Startup script for mongod
  3. # chkconfig: 35 85 15
  4. # description: Mongo is a scalable, document-oriented database.
  5. # processname: mongod
  6. # config: /etc/mongod.conf
  7. . /etc/rc.d/init.d/functions
  8. # NOTE: if you change any OPTIONS here, you get what you pay for:
  9. # this script assumes all options are in the config file.
  10. CONFIGFILE="/etc/mongod.conf"
  11. OPTIONS=" -f $CONFIGFILE"
  12. mongod=${MONGOD-/usr/local/mongodb/bin/mongod}
  13. MONGO_USER=mongod
  14. MONGO_GROUP=mongod
  15. # All variables set before this point can be overridden by users, by
  16. # setting them directly in the SYSCONFIG file. Use this to explicitly
  17. # override these values, at your own risk.
  18. SYSCONFIG="/etc/sysconfig/mongod"
  19. if [ -f "$SYSCONFIG" ]; then
  20. . "$SYSCONFIG"
  21. fi
  22. # Handle NUMA access to CPUs (SERVER-3574)
  23. # This verifies the existence of numactl as well as testing that the command works
  24. NUMACTL_ARGS="--interleave=all"
  25. if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  26. then
  27. NUMACTL="numactl $NUMACTL_ARGS"
  28. else
  29. NUMACTL=""
  30. fi
  31. # things from mongod.conf get there by mongod reading it
  32. PIDFILEPATH="`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' \"$CONFIGFILE\" | tr -d \"[:blank:]\\"'\" | awk -F'#' '{print $1}'`"
  33. PIDDIR=`dirname $PIDFILEPATH`
  34. start()
  35. {
  36. # Make sure the default pidfile directory exists
  37. if [ ! -d $PIDDIR ]; then
  38. install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR
  39. fi
  40. # Make sure the pidfile does not exist
  41. if [ -f "$PIDFILEPATH" ]; then
  42. echo "Error starting mongod. $PIDFILEPATH exists."
  43. RETVAL=1
  44. return
  45. fi
  46. # Recommended ulimit values for mongod or mongos
  47. # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
  48. #
  49. ulimit -f unlimited
  50. ulimit -t unlimited
  51. ulimit -v unlimited
  52. ulimit -n 64000
  53. ulimit -m unlimited
  54. ulimit -u 64000
  55. ulimit -l unlimited
  56. echo -n $"Starting mongod: "
  57. daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
  58. RETVAL=$?
  59. echo
  60. [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
  61. }
  62. stop()
  63. {
  64. echo -n $"Stopping mongod: "
  65. mongo_killproc "$PIDFILEPATH" $mongod
  66. RETVAL=$?
  67. echo
  68. [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
  69. }
  70. restart() {
  71. stop
  72. start
  73. }
  74. # Send TERM signal to process and wait up to 300 seconds for process to go away.
  75. # If process is still alive after 300 seconds, send KILL signal.
  76. # Built-in killproc() (found in /etc/init.d/functions) is on certain versions of Linux
  77. # where it sleeps for the full $delay seconds if process does not respond fast enough to
  78. # the initial TERM signal.
  79. mongo_killproc()
  80. {
  81. local pid_file=$1
  82. local procname=$2
  83. local -i delay=300
  84. local -i duration=10
  85. local pid=`pidofproc -p "${pid_file}" ${procname}`
  86. kill -TERM $pid >/dev/null 2>&1
  87. usleep 100000
  88. local -i x=0
  89. while [ $x -le $delay ] && checkpid $pid; do
  90. sleep $duration
  91. x=$(( $x + $duration))
  92. done
  93. kill -KILL $pid >/dev/null 2>&1
  94. usleep 100000
  95. checkpid $pid # returns 0 only if the process exists
  96. local RC=$?
  97. [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown"
  98. RC=$((! $RC)) # invert return code so we return 0 when process is dead.
  99. return $RC
  100. }
  101. RETVAL=0
  102. case "$1" in
  103. start)
  104. start
  105. ;;
  106. stop)
  107. stop
  108. ;;
  109. restart|reload|force-reload)
  110. restart
  111. ;;
  112. condrestart)
  113. [ -f /var/lock/subsys/mongod ] && restart || :
  114. ;;
  115. status)
  116. status $mongod
  117. RETVAL=$?
  118. ;;
  119. *)
  120. echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
  121. RETVAL=1
  122. esac
  123. exit $RETVAL