Skip to content

Instantly share code, notes, and snippets.

@soomtong
Created August 28, 2014 07:40
Show Gist options
  • Select an option

  • Save soomtong/fb352256b75a7373167f to your computer and use it in GitHub Desktop.

Select an option

Save soomtong/fb352256b75a7373167f to your computer and use it in GitHub Desktop.
mongo db manage script use /usr/local/bin/mdb with excution flag
#!/bin/bash
if [ -z $1 ] ; then
echo "Usage: $0 [start|stop|restart] "
exit 1
fi
# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common
# Set up some defaults
DBPATH='/usr/local/var/mongodb'
LOGPATH='/usr/local/var/log/mongodb/mongod.log'
MONGOD_PORT=27017
StartService(){
/usr/local/bin/mongod run --dbpath=$DBPATH --logpath=$LOGPATH --port $MONGOD_PORT > /dev/null 2>&1 &
}
StopService() {
pidfile=$DBPATH/mongod.lock
# If the lockfile exists, it contains the PID
if [ -e $pidfile ]; then
pid=`cat $pidfile`
fi
# If we don't have a PID, check for it
if [ "$pid" == "" ]; then
pid=`/usr/sbin/lsof -i tcp:$MONGOD_PORT | tail -1 | awk '{print $2}'`
fi
# If we've found a PID, let's kill it
if [ "$pid" != "" ]; then
kill -15 $pid
fi
}
RestartService() {
StopService
sleep 3
StartService
}
RunService $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment