Skip to content

Instantly share code, notes, and snippets.

@m-szk
Created October 23, 2011 01:33
Show Gist options
  • Save m-szk/1306735 to your computer and use it in GitHub Desktop.
Save m-szk/1306735 to your computer and use it in GitHub Desktop.
a bash script for start or stop mongodb.
#!/bin/bash
mongod=/usr/local/mongodb/bin/mongod
mongod_data=/Users/michito/work/mongodb_data
mongod_log=/Users/michito/work/mongodb_log/mongodb.log
prog=mongod.sh
RETVAL=0
stop() {
grep_mongo=`ps aux | grep -v grep | grep "${mongod}"`
if [ ${#grep_mongo} -gt 0 ]
then
echo "Stop MongoDB."
PID=`ps x | grep -v grep | grep "${mongod}" | awk '{ print $1 }'`
`kill -2 ${PID}`
RETVAL=$?
else
echo "MongoDB is not running."
fi
}
start() {
grep_mongo=`ps aux | grep -v grep | grep "${mongod}"`
if [ -n "${grep_mongo}" ]
then
echo "MongoDB is already running."
else
echo "Start MongoDB."
`${mongod} --dbpath ${mongod_data} --logpath ${mongod_log} --fork --logappend`
RETVAL=$?
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $prog {start|stop|restart}"
exit 1
esac
exit $RETVAL
@marcmaxson
Copy link

How would I modify this script to run mongod with these parameters?
nohup nice mongo64/bin/mongod --port 14703 --auth --dbpath /home/djotjog/webapps/mongo/data/ &

@sam2332
Copy link

sam2332 commented Jun 24, 2015

look for this line
${mongod} --dbpath ${mongod_data} --logpath ${mongod_log} --fork --logappend
and modify for your prams ${} are used for variable substitution

@cuculala
Copy link

If you could provide me resource to a tutorial for chef deployment of MongoDB replica sets

Copy link

ghost commented Aug 12, 2015

Thanks for the code. It is very useful. But on line 14 i would change ps x to ps ax. Because in my case it displayed nothing if I use ps x

@patrick0585
Copy link

patrick0585 commented May 11, 2017

Nice script, I also created an own script to start, stop, restart an mongodb-server under unix.
Maybe you could take a look: Mongodb_Start_Stop

One tipp for your grep command:
you used

grep -v grep

easier it would be if you use this:

grep_mongo=ps aux | grep [m]ongod
Then you get only one result with the right process of the mongod-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment