Mongodb database adding System Service tutorial, mongodb System Service
1. Create the mongod file in the/etc/init. d directory and grant the correct permissions:
root@ubuntu:/etc/init.d# touch mongodroot@ubuntu:/etc/init.d# chmod 755 mongod
2. Edit the mongod File
#!/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 INFOstart_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" ;;esacexit 0
3. Add to System Service
root@ubuntu:/etc/init.d# update-rc.d mongod defaults
You can use the command to manage the mongod service.
root@ubuntu:/etc/init.d# service mongod start