Linux under MongoDB service operation Shell Script

Source: Internet
Author: User
Tags mongodb


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.