8

I can start mongodb on terminal via command

./mongod

It starts the mongodb server and then display me information that server is running on this port. but It does not give my terminal back. How can I start mongodb and can get terminal back so mongodb is running the background.

Also how to shutdown if its running in background

coure2011
  • 40,286
  • 83
  • 216
  • 349

2 Answers2

7

Use

./mongod --fork

or

./mongod &

To shutdown you have to send it a TERM signal.

ps aux | grep mongod - to find a PID

kill -TERM PID - send it a TERM signal, and using the first example we can use the PID file:

kill -TERM $(cat /var/run/mongodb/mongod.pid)

Also you can shut it down from the shell.

$ ./mongo
> use admin
> db.shutdownServer()

--

And another method:

./mongod --fork --pidfilepath /var/run/mongodb/mongod.pid

then (please notice the ticks around the cat)

kill -9 `cat /var/run/mongodb/mongod.pid`
b01
  • 4,076
  • 2
  • 30
  • 30
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
1
./mongod &

You will see a number in the output, something similar to

[1]+ ./mongod &

To kill the process execute a kill %1 where 1 is the number between the angular brackets.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • I don't understand what the '&' part does and what is it called. Care to explain or link me to a resourceful link? – dominicbri7 Jul 02 '15 at 14:48
  • 1
    It lets you run the command in background, so you can run a script that takes a lot of time and meanwhile do something else. On the subject: http://www.tldp.org/LDP/abs/html/x9644.html – Alberto Zaccagni Jul 02 '15 at 15:01