The mongodb service monitoring script of under Linux starts the service
- 2020-05-14 05:43:39
- OfStack
A few days ago, one of my development colleagues came to me and said that his mongodb of the test environment was often down and asked me to write a script to monitor or resurrect it. I find it strange that the test environment is so unloaded that there must be some unusual reason for it to fail so often.
I ran over and looked at the log and found that there was an stop record. I was wondering if he could stop by himself without anyone to operate it. This is clearly not dead, so I went to history to see my colleague's startup command:
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/data/mongodb.log --logappend &
So that's it! Since he didn't start nohup, mongodb will automatically quit whenever his terminal goes offline or shuts down! The solution is simple: start as follows:
nohup /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/data/mongodb.log --logappend >/dev/null 2>&1 &
It's a pain in the ass, so it's nice to find an mongodb service script on the web:
#!/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 get 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 "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f $lockfile ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL
To save the code to/etc/init d/mongodb, then use the chmod + x/etc/init d/mongodb add execute permissions.
Now you can use the service command to control mongodb:
service mongodb start|stop|restart
# or
/etc/init.d/mongodb start|stop|restart
Very simple, post to blog entry 1 in case you need it.