#!/bin/sh
#
# MongoDB daemon control script.
# Written for Slackware Linux by Jose Bovet Derpich <[email protected]>.

BIN=/opt/mongodb/bin/mongod
PID=/var/run/mongod.pid
LOG=/var/log/mongodb
DBPATH=/opt/mongodb/data/db

mongod_start() {
  if [ -s $PID ]; then
    echo "mongod appears to already be running?"
    exit 1
  fi

  echo "Starting mongo server daemon..."
  if [ -x $BIN ]; then
    $BIN --auth --dbpath=$DBPATH --pidfilepath=$PID --logpath=$LOG --fork --bind_ip 127.0.0.1
  fi
}

mongod_stop() {
  echo "Shutdown mongod..."
  if [ -r $PID ]; then
    kill -TERM $(cat $PID)
    rm $PID
  fi
}

mongod_restart() {
  mongod_stop
  sleep 3
  mongod_start
}

case "$1" in
  start)
    mongod_start
    ;;
  stop)
    mongod_stop
    ;;
  restart)
    mongod_restart
    ;;
  *)
  echo "usage: `basename $0` {start|stop|restart}"
esac

Gist Link


Jose Bovet Derpich

Linux and Coffee Lover, Father of @SlackwareSecAdv - Software Engineer at Walmart