使用脚本管理mongodb服务

[root@MongoDB ~]# cat /etc/init.d/mongod  
#!/bin/bash
 
 
MONGODIR=/usr/local/mongodb
MONGOD=$MONGODIR/bin/mongod
MONGOCONF=/etc/mongodb.conf
InfoFile=/tmp/start.mongo
 
. /etc/init.d/functions 
 
status(){
  PID=`awk 'NR==2{print $NF}' $InfoFile`
  Run_Num=`ps -p $PID|wc -l`
  if [ $Run_Num -eq 2 ]; then
    echo "MongoDB is running"
  else 
    echo "MongoDB is shutdown"
    return 3
  fi
}
 
start() {
  status &>/dev/null
  if [ $? -ne 3 ];then 
    action "启动MongoDB,服务运行中..." /bin/false 
    exit 2
  fi
  $MONGOD -f $MONGOCONF
  if [ $? -eq 0 ];then 
    action "启动MongoDB" /bin/true
  else
    action "启动MongoDB" /bin/false
  fi
}
 
 
stop() {
  $MONGOD -f $MONGOCONF --shutdown
  if [ $? -eq 0 ];then
    action "停止MongoDB" /bin/true
  else 
    action "停止MongoDB" /bin/false
  fi 
}
 
 
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    sleep 2
    start
    ;;
  status)
    status
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac
原文地址:https://www.cnblogs.com/liang545621/p/12605846.html