#!/bin/bash # mongod - Startup script for mongod # chkconfig: 35 82 15 # description: Mongo is a scalable, document-oriented database. # processname: mongod # config: /etc/mongod.conf # pidfile: /var/run/mongo/mongod.pid . /etc/rc.d/init.d/functions . /home/config/cluster_core_functions # NOTE: if you change any OPTIONS here, you get what you pay for: # this script assumes all options are in the config file. CONFIGFILE_MONGO="/etc/mongod.conf" CONFIGFILE_SHARD="/etc/mongod_shard.conf" OPTIONS=" -f $CONFIGFILE_MONGO" SYSCONFIG="/etc/sysconfig/mongod" PIDFILEPATH="" is_mongo_standalone_mode retval_standalone=$? if [ "$retval_standalone" -eq "$ENABLE_SERVICE" ] ; then PIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE_MONGO" | tr -d "[:blank:]\"'"` else PIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE_SHARD" | tr -d "[:blank:]\"'"` fi mongod=${MONGOD-/usr/bin/mongod} MONGO_USER=mongod MONGO_GROUP=mongod if [ -f "$SYSCONFIG" ]; then . "$SYSCONFIG" fi PIDDIR=`dirname $PIDFILEPATH` # Handle NUMA access to CPUs (SERVER-3574) # This verifies the existence of numactl as well as testing that the command works NUMACTL_ARGS="--interleave=all" if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null then NUMACTL="numactl $NUMACTL_ARGS" else NUMACTL="" fi start() { # Make sure the default pidfile directory exists if [ ! -d $PIDDIR ]; then install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR fi is_mongo_standalone_mode retval_standalone=$? if [ "$retval_standalone" -eq "$ENABLE_SERVICE" ] ; then mongo_config_tuning $CONFIGFILE_MONGO if [ $? -eq 0 ]; then OPTIONS=" -f $CONFIGFILE_MONGO" pspid=`ps -eo pid,cmd | grep "/usr/bin/mongod" | grep -v "grep" | awk '{print $1}'` else RETVAL=1 return $RETVAL fi else is_mongod_configured retval_mongod=$? if [ "$retval_mongod" -eq "$DISABLE_SERVICE" ] ; then RETVAL=1 return $RETVAL fi OPTIONS=" -f $CONFIGFILE_SHARD" pspid=`ps -eo pid,cmd | grep "mongod" | grep -v "status" | grep -v "grep" | grep "mongod_shard" | awk '{print $1}'` fi # Recommended ulimit values for mongod or mongos # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings # ulimit -f unlimited ulimit -t unlimited ulimit -v unlimited ulimit -n 64000 ulimit -m unlimited ulimit -u 64000 echo -n $"Starting mongod: " if [ ! -z "$pspid" ]; then if [ -f "$PIDFILEPATH" ]; then pid_in_file=`cat "$PIDFILEPATH"` if [ "$pspid" != "$pid_in_file" ] ;then echo "$pspid" > "$PIDFILEPATH" chown mongod:mongod "$PIDFILEPATH" fi fi RETVAL=0 success echo else daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1" RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod fi } stop() { echo -n $"Stopping mongod: " mongo_killproc "$PIDFILEPATH" $mongod RETVAL=$? rm -f /var/lib/mongodb_wiredTiger/mongod.lock > /dev/null 2>&1 echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod } restart () { stop start } # Send TERM signal to process and wait up to 300 seconds for process to go away. # If process is still alive after 300 seconds, send KILL signal. # Built-in killproc() (found in /etc/init.d/functions) is on certain versions of Linux # where it sleeps for the full $delay seconds if process does not respond fast enough to # the initial TERM signal. mongo_killproc() { local pid_file=$1 local procname=$2 local -i delay=300 local -i duration=10 local pid=`pidofproc -p "${pid_file}" ${procname}` kill -TERM $pid >/dev/null 2>&1 usleep 100000 local -i x=0 while [ $x -le $delay ] && checkpid $pid; do sleep $duration x=$(( $x + $duration)) done kill -KILL $pid >/dev/null 2>&1 usleep 100000 checkpid $pid # returns 0 only if the process exists local RC=$? [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown" RC=$((! $RC)) # invert return code so we return 0 when process is dead. return $RC } RETVAL=0 case "$1" in start) start ;; stop) stop ;; restart|reload|force-reload) restart ;; condrestart) [ -f /var/lock/subsys/mongod ] && restart || : ;; status) if ! is_mongo_standalone_mode ; then mypid=$(status $mongod | egrep -o '[0-9]+' ) if [ ! -z "$mypid" ];then pspid=`ps -eo pid,cmd | grep "mongod" | grep -v "status" | grep -v "grep" | grep "mongod_shard" | awk '{print $1}'` running_pid=`echo "$mypid" | grep -w "$pspid"` RETVAL=$? else RETVAL=1 fi if [ $RETVAL -eq 0 ]; then echo "mongod (pid $running_pid) is running..." else echo "mongod is stopped" RETVAL=1 fi else status $mongod RETVAL=$? fi ;; *) echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" RETVAL=1 esac exit $RETVAL