Sunday, 5 June 2016

How to repair the MongoDB with bash script?


repair_and_restart_mongod(){

  if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi

  ENABLE_MONGODB="yes"
  if [ "x$ENABLE_MONGODB" = "xyes" ]; then
        if [ -f $MONGODB_HOME/mongod.lock ]; then
                rm $MONGODB_HOME/mongod.lock >> /dev/null
                sudo -u mongodb /usr/bin/mongod --config /etc/mongodb.conf --repair >> /dev/null
        fi
        #exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf
        service mongodb start >> /dev/null  2>&1
  fi
}

Tested under Ubuntu Server 12.x& 14.x

How to check the MongoDB Status in bash script?


is_mongod_running(){
PID=$(cat $MONGODB_HOME/mongod.lock)
        status=$(service mongodb status 2>&1)
        echo $status | grep -q "mongodb start/running, process.*"

        if [ $? -eq 0 ] && [ "x$PID" != "x" ] ;then     #&& kill -0 $PID 2>/dev/null; then
                _status=0 #echo "mongod is running"
        else
                _status=1 #echo "mongod is not running"
        fi
        return $_status
}


Note: Tested under Ubuntu Server 2.x& 14.x