How to write a mongodb service monitoring script under Linux

WBOY
Release: 2023-06-02 17:40:04
forward
1411 people have browsed it

到history中看下启动命令:

复制代码 代码如下:

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

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

复制代码 代码如下:

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

这样敲命令也着实苦逼,所以从网上找了一个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
Copy after login

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

复制代码 代码如下:

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template