A few days ago, a development colleague found me saying that the mongodb of his test environment often hung up and asked me to write a monitor or resurrection script. I think it is strange that the test environment is not a load, often hanging off must have unconventional reasons.
Ran past looked at the log, found that there is a stop record, I was puzzled, no one to operate his own stop. This is obviously not dead, so went to history to see the next colleague's start command:
/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data/--logpath=/data/mongodb.log--logappend &
I see! Because he did not start with nohup, so as long as his terminal offline or shut down, MongoDB will automatically quit! The solution is simple, starting with the following:
Nohup/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data/--logpath=/data/mongodb.log--logappend >/ Dev/null 2>&1 &
It's hard to knock orders, so it's much more comfortable to find a MongoDB service script from the Internet:
#!/bin/sh
#
#mongod-startup script for Mongod
#
# Chkconfig:-85 15
# DESCRIPTION:MONGODB database.
# Processname:mongod
# Source Function Library
. /etc/rc.d/init.d/functions
# things from mongod.conf to there by Mongod reading it
# OPTIONS
options= "--dbpath=/home/data/mongodb/--logpath=/home/data/mongodb/mongodb.log--logappend &"
#mongod
Mongod= "/usr/local/mongodb/bin/mongod"
Lockfile=/var/lock/subsys/mongod
Start ()
{
Echo-n $ "Starting Mongod:"
Daemon $mongod $OPTIONS
Retval=$?
Echo
[$RETVAL-eq 0] && Touch $lockfile
}
Stop ()
{
Echo-n $ "Stopping Mongod:"
Killproc $mongod-quit
Retval=$?
Echo
[$RETVAL-eq 0] && rm-f $lockfile
}
Restart () {
Stop
Start
}
Ulimit-n 12000
Retval=0
Case "$" in
Start
Start
;;
Stop
Stop
;;
Restart|reload|force-reload)
Restart
;;
Condrestart)
[F $lockfile] && Restart | | :
;;
Status
Status $mongod
Retval=$?
;;
*)
echo "Usage: $ {Start|stop|status|restart|reload|force-reload|condrestart}"
Retval=1
Esac
Exit $RETVAL
Save the code to/ETC/INIT.D/MONGODB, and then use chmod +x/etc/init.d/mongodb to add execution permissions.
Now, you can use the Service command to control MongoDB:
Service MongoDB Start|stop|restart
#或
/etc/init.d/mongodb Start|stop|restart
Very simple, posted to the blog to record, in case of a rainy moment.
Original from: http://zhangge.net/5044.html