摘要:#!/BIN/bashBIN_PATH=$(cd "$(dirname "$0")"; pwd)cd $BIN_PATHumask 077source ~/.bash_profileMONGO_PWD=$(base64 -d
#!/BIN/bash
BIN_PATH=$(cd "$(dirname "$0")"; pwd)
cd $BIN_PATH
umask 077
source ~/.bash_profile
MONGO_PWD=$(base64 -d
start {
sleep 1
if type numactl >/dev/null 2>&1; then
echo "Use numactl pattern starting"
numactl --interleave=all $UYUN_ROOT_DIR/mongodb/mongodb/bin/mongod -f $UYUN_ROOT_DIR/mongodb/mongodb/mongodb.conf
else
$UYUN_ROOT_DIR/mongodb/mongodb/bin/mongod -f $UYUN_ROOT_DIR/mongodb/mongodb/mongodb.conf
fi
}
stop {
if [[ -n "$(ps aux | grep 'mongodb/mongodb.conf' | grep -v grep)" ]]; then
dbpath=$(cat $UYUN_ROOT_DIR/mongodb/mongodb/mongodb.conf | grep dbpath | awk -F '=' '{print $2}' | xargs echo)
$UYUN_ROOT_DIR/mongodb/mongodb/bin/mongod --dbpath=$dbpath --shutdown
else
echo "mongodb has stopped!"
fi
}
status {
pid=`ps -ef | grep 'mongodb/bin/mongod' | grep -vE "grep|manage" | awk '{print $2}'`
if [[ ! -z "$pid" ]]; then
node_num=`$UYUN_ROOT_DIR/mongodb/mongodb/bin/mongo -utest -p$MONGO_PWD --authenticationDatabase admin --eval "rs.status;" | grep 'stateStr' | wc -l`
on_num=`$UYUN_ROOT_DIR/mongodb/mongodb/bin/mongo -utest -p$MONGO_PWD --authenticationDatabase admin --eval "rs.status;" | grep -E 'PRIMARY|SECONDARY|ARBITER' | wc -l`
half=`expr $node_num / 2 + 1`
if [[ "$on_num" -ge "$half" ]]; then
echo "mongodb service available!"
exit 0
else
echo "mongodb service unavailable!"
exit 1
fi
else
echo "mongodb not running!"
exit 3
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
status)
status
;;
esac
来源:吕军军侃万事