1. 程式人生 > >Mongodb--新增系統服務

Mongodb--新增系統服務

1.在/etc/init.d目錄下建立mongod檔案,並賦予正確的許可權:

root@ubuntu:/etc/init.d# touch mongod
root@ubuntu:/etc/init.d# chmod 755 mongod

2.編輯mongod檔案

#!/bin/sh
### BEGIN INIT INFO
# Provides:     mongod
# Required-Start:   $local_fs $syslog
# Required-Stop:    $local_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     
# Short-Description: mongodb service ### END INIT INFO start_mongodb() { ps -ef | grep -v "grep" | grep "/usr/local/mongodb/bin/mongod" if [ $? -eq 0 ];then echo "mongodb is in running!" return 0 fi /usr/local/mongodb/bin/mongod --auth & } stop_mongodb() { /usr/local/mongodb/bin/mongod --shutdown if
[ $? -eq 0 ];then echo "stop mongodb service successfully!" else echo "stop mongodb service failed!" fi } query_status() { ps -ef | grep -v "grep" | grep "/usr/local/mongodb/bin/mongod" if [ $? -eq 0 ];then echo "mongodb is in running!" else echo "mongodb is not in running!"
fi } case "$1" in start) start_mongodb ;; stop) stop_mongodb ;; restart) stop_mongodb start_mongodb ;; status) query_status ;; *) echo "usage: service mongodb start|stop|restart|status" ;; esac exit 0

3.新增到系統服務中

root@ubuntu:/etc/init.d# update-rc.d mongod defaults

就可以使用命令管理mongod服務了

root@ubuntu:/etc/init.d# service mongod start