Mongodb SysV开机自启动脚本

之前发表过一篇MongoDB安装部署文章,在这里再给大家附上MongDB SysV开机自启动脚本,废话不多说,各位看官往下看:

MongoDB SysV开机自启动脚本

[root@localhost ~]# vi /etc/init.d/mongodb
#!/bin/sh
#
# mongod - Startup script for mongodb
# chkconfig: - 85 15
# Description: Mongo database
# Processname: mongod
# Config: /etc/mongodb/mongodb.conf
# Pidfile: /var/run/mongodb/mongod.pid
# Author:沉醉寒风
# Email:liaoronghui@vip.qq.com
# Blog:http://www.liaoronghui.com
# Version:version 1.0

# Source function library.
. /etc/rc.d/init.d/functions

binfile=/usr/local/mongodb/bin/mongod
config=/etc/mongodb/mongodb.conf
lock=/usr/local/mongodb/data/mongod.lock
prog=$(basename $binfile)
RETVAL=0

start() {
    [ -x $binfile ] || exit 5
    [ -f $config ] || exit 6
    if [ -e $lock ];then
        status $prog
        exit 1
    fi
    echo -n $"Starting $prog: "
    daemon "$binfile -f $config &>/dev/null"
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lock
    return $RETVAL
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $lock
    return $RETAVAL
}
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
status)
    status $prog
    ;;
*)
    echo $"Usage: $0 {start|stop|status|restart}"
    RETVAL=1
esac

exit $RETVAL

设置开机自启动

[root@localhost ~]# chmod 755 /etc/init.d/mongodb         #给脚本赋予执行权限
[root@localhost ~]# chkconfig --add mongodb               #加入到chkconfig管理
[root@localhost ~]# chkconfig mongodb on                  #设置开机自启动