MongoDB集群service启动命令脚本(CentOS)
这里是之前写的一个MongoDB集群的启动脚本,做下备份。
#!/bin/bash
# iw - Startup script for mongod and mongos
# chkconfig: 35 85 15
# description: Mongo is a scalable, document-oriented database.
# processname: iw
################################################
# MongoDB 集群启动脚本
# @author hewei (hewei@itfsw.com)
# @date 2015.11.16
################################################
# Source function library.
. /etc/init.d/functions
######################################### 配置信息 #####################################
# MongoDB 配置文件目录
confDir="/etc/mongodb"
pidDir="/var/run/mongodb"
mongod=${MONGOD-/usr/bin/mongod}
mongos=${MONGOS-/usr/bin/mongos}
MONGO_USER=mongod
MONGO_GROUP=mongod
# 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|stop|restart|mongo_killproc 方法 #####################################
mongos_start()
{
# Make sure the default pidfile directory exists
if [ ! -d $pidDir ]; then
install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $pidDir
fi
# 判断lock
if [ ! -f /var/lock/subsys/mongos ]; then
# 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 mongos: "
for file in `ls $confDir`
do
# 判断是不是mongos配置文件, 启动
if [ -f "${confDir}/${file}" ] && [ ${#file} -gt 5 ] && [ ${file:0:6} = "mongos" ]; then
count=0
while [ $count -lt 5 ]
do
daemon --user "$MONGO_USER" --check $mongos "$NUMACTL $mongos --config ${confDir}/${file} >/dev/null 2>&1"
RETVAL=$?
# 如果某些启动失败,则120s重试
if [ $RETVAL -ne 0 ]; then
sleep 120s
((count=count+1))
else
count=9999
fi
done
echo -n " ${confDir}/${file}(${RETVAL})"
# 如果某些启动失败,则停止
if [ $RETVAL -ne 0 ]; then
break;
fi
fi
done
fi
# lock
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongos
}
start()
{
# Make sure the default pidfile directory exists
if [ ! -d $pidDir ]; then
install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $pidDir
fi
# 关闭transparent_hugepage
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
# 判断lock
if [ ! -f /var/lock/subsys/mongod ]; then
# 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 mongodb: "
for file in `ls $confDir`
do
# 判断是不是shard或者config server配置文件, 启动
if [ -f "${confDir}/${file}" ] && [ ${#file} -gt 5 ] && [ ${file:0:6} != "mongos" ]; then
daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod --config ${confDir}/${file} >/dev/null 2>&1"
RETVAL=$?
echo -n " ${confDir}/${file}(${RETVAL})"
# 如果某些启动失败,则停止
if [ $RETVAL -ne 0 ]; then
break;
fi
fi
done
# 启动mongos
if [ $RETVAL -eq 0 ]; then
mongos_start
fi
fi
# lock
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}
mongos_stop()
{
echo "Stopping mongos: "
# 关闭mongod
for file in `ls $confDir`
do
# 判断是不是shard或者config server配置文件, 启动
if [ -f "${confDir}/${file}" ] && [ ${#file} -gt 5 ] && [ ${file:0:6} = "mongos" ]; then
# 获取pid文件
PIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "${confDir}/${file}" | tr -d "[:blank:]\"'"`
mongo_killproc "$PIDFILEPATH" $mongos
RETVAL=$?
echo -n " ${confDir}/${file}(${RETVAL})"
# 如果某些启动失败,则停止
if [ $RETVAL -ne 0 ]; then
break;
fi
fi
done
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongos
}
stop()
{
echo "Stopping mongod: "
# 关闭mongos
mongos_stop
# 关闭mongod
for file in `ls $confDir`
do
# 判断是不是shard或者config server配置文件, 启动
if [ -f "${confDir}/${file}" ] && [ ${#file} -gt 5 ] && [ ${file:0:6} != "mongos" ]; then
# 获取pid文件
PIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "${confDir}/${file}" | tr -d "[:blank:]\"'"`
mongo_killproc "$PIDFILEPATH" $mongod
RETVAL=$?
echo -n " ${confDir}/${file}(${RETVAL})"
# 如果某些启动失败,则停止
if [ $RETVAL -ne 0 ]; then
break;
fi
fi
done
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 ] && [ -f /var/lock/subsys/mongos ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL
