停止脚本
#!/bin/bash
pid=`ps -o pid,command ax | grep mongo | awk '!/awk/ && !/grep/ {print $1}'`;
if [ "${pid}" != "" ]; then
kill -2 ${pid};
fi
#
# init.d / servicectl compatibility (openSUSE)
#
if [ -f /etc/rc.status ]; then
. /etc/rc.status
rc_reset
fi
#
# Source function library.
#
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
#
#
#
pids=("shard0" "shard1" "shard2" "config" "mongos")
#pids=("config")
#
MONGO_USER=root
MONGO_GROUP=root
MONGO_HOME="/usr/local/mongo"
#
mongod="$MONGO_HOME/bin/mongod"
mongos="$MONGO_HOME/bin/mongos"
#
#
#
MAX_OPEN_FILES=65535
MAX_MAP_COUNT=262144
start() {
ini
echo "begin start"
ulimit -f unlimited
ulimit -t unlimited
ulimit -v unlimited
ulimit -n 64000
ulimit -m unlimited
ulimit -u 64000
if [ -n "$MAX_OPEN_FILES" ]; then
ulimit -n $MAX_OPEN_FILES
fi
if [ -n "$MAX_LOCKED_MEMORY" ]; then
ulimit -l $MAX_LOCKED_MEMORY
fi
if [ -n "$MAX_MAP_COUNT" -a -f /proc/sys/vm/max_map_count ]; then
sysctl -q -w vm.max_map_count=$MAX_MAP_COUNT
fi
for pgName in ${pids[@]};
do
pidfile="$MONGO_HOME/$pgName.pid"
pgLock=/var/lock/subsys/mongodb_$pgName
if [ -n "$pidfile" ] && [ ! -e "$pidfile" ]; then
touch "$pidfile" && chown "$MONGO_USER":"$MONGO_GROUP" "$pidfile"
fi
echo $pidfile
# CONF_FILE setting was removed
pgConfigFile="$MONGO_HOME/conf/$pgName.conf"
if [ ! -f "$pgConfigFile" ]; then
echo "pgConfigFile $pgName 没有找到配置文件 $pgConfigFile"
exit 1
fi
OPTIONS=" -f $pgConfigFile"
echo $mongod
echo $OPTIONS
echo -n $"Starting mongod: "
if [ "$pgName" = "mongos" ]; then
echo "start mongos --0"
daemon --user "$MONGO_USER" --check $mongos "$NUMACTL $mongos $OPTIONS >/dev/null 2>&1"
else
daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
fi
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $pgLock
done
}
stop() {
for pgName in ${pids[@]};
do
pidfile="$MONGO_HOME/$pgName.pid"
pgLock=/var/lock/subsys/mongodb_$pgName
echo $pidfile
# stop it here, often "killproc $prog"
pgId=`cat $pidfile`
echo -n "Stopping MongoDB server id= $pgId"
if [ ! -z "$pgId" ]; then
kill -15 $pgId
echo -n "Stopped -----> $pgId"
fi
retval=$?
echo
[ $retval -eq 0 ] && rm -f $pgLock
done
}
restart () {
stop
start
}
stopall()
{
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
#echo "$MONGOPID"
echo -n "Stopping MongoDB server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
echo -n "Stopped -----> $MONGOPID"
fi
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $MONGO_LCOK
}
ini(){
###
echo "ini"
if [ -d /sys/kernel/mm/transparent_hugepage ]; then
thp_path=/sys/kernel/mm/transparent_hugepage
elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
thp_path=/sys/kernel/mm/redhat_transparent_hugepage
else
return 0
fi
echo 'never' > ${thp_path}/enabled
echo 'never' > ${thp_path}/defrag
re='^[0-1]+$'
if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
then
# RHEL 7
echo 0 > ${thp_path}/khugepaged/defrag
else
# RHEL 6
echo 'no' > ${thp_path}/khugepaged/defrag
fi
unset re
unset thp_path
# disable NUMA
sysctl -w vm.zone_reclaim_mode=0
###
}
RETVAL=0
case "$1" in
start)
start
;;
stop)
stop
;;
stopall)
stopall
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f $MONGO_LCOK ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL