Home  >  Article  >  Operation and Maintenance  >  How to write a mongodb service monitoring script under Linux

How to write a mongodb service monitoring script under Linux

WBOY
WBOYforward
2023-06-02 17:40:041038browse

到history中看下启动命令:

复制代码 代码如下:

/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/data/mongodb.log --logappend &

原来如此!因为他没有用nohup启动,所以只要他的终端离线或者关闭,mongodb就会自动退出了!解决办法很简单,如下启动即可:

复制代码 代码如下:

nohup /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/data/mongodb.log --logappend >/dev/null 2>&1 &

这样敲命令也着实苦逼,所以从网上找了一个mongodb服务脚本就舒服多了:

#!/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

将代码保存到 /etc/init.d/mongodb,然后使用 chmod +x /etc/init.d/mongodb 添加执行权限。
现在,就可以使用 service 命令来控制mongodb了:

复制代码 代码如下:

service mongodb start|stop|restart
#或
/etc/init.d/mongodb start|stop|restart

The above is the detailed content of How to write a mongodb service monitoring script under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete