Skip to content

Instantly share code, notes, and snippets.

@renebakx
Created November 20, 2011 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renebakx/1380166 to your computer and use it in GitHub Desktop.
Save renebakx/1380166 to your computer and use it in GitHub Desktop.
starting mongo on OSX
#!/bin/bash
#MongoDB startup script with status and proper lockfile removing
#Version 1.0.1 20-nov-2011 Rene Bakx (rene@71media.net)
#Created on OS-X 10.7 but should work on other versions
if [ -z $1 ] ; then
echo "Usage: $0 [start|stop|restart|status] "
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="/Users/rene/Sites/mongodb/data"
LOGPATH="/Users/rene/Sites/mongodb/mongodb.log"
MONGOD_PORT=27017
PIDFILE=$DBPATH/mongod.lock
pid=""
StartService(){
/Users/rene/Sites/mongodb/bin/mongod run --dbpath=$DBPATH --logpath=$LOGPATH --port $MONGOD_PORT > /dev/null 2>&1 &
sleep 1
getPID
echo "Started mongo on pid $pid"
}
getPID(){
# 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
# There's no pid, mongo is not running
if [ "$pid" == "" ]; then
echo "Mongo is not running"
exit 0;
fi
}
StopService() {
getPID
# If we've found a PID, let's kill it
if [ "$pid" != "" ]; then
kill -3 $pid
echo "Stopped mongo on pid $pid"
if [ -e $PIDFILE ]; then
pid=`rm $PIDFILE`
fi
fi
}
RestartService() {
StopService
sleep 3
StartService
}
StatusService(){
getPID
# If we've found a PID, let's kill it
if [ "$pid" != "" ]; then
echo "Mongo is running on pid $pid"
fi
}
case $1 in
start ) StartService ;;
stop ) StopService ;;
restart) RestartService ;;
status) StatusService ;;
* ) echo "$0: unknown argument: $1";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment