49

I had installed MongoDB few days back on my Mac and am not sure how I installed, but now how do I check if MongoDB is up and running in my system?

Maëlan
  • 3,586
  • 1
  • 15
  • 35
nshetty
  • 1,097
  • 2
  • 10
  • 7
  • [`top`](http://www.unixtop.org/man.shtml)? Or `ps aux | grep mongod`. Anything that shows running processes. Trying to connect is also a start. I'm sensing a question within a question here. – Blakes Seven Jul 22 '15 at 10:57
  • Thank you very much for replying, sorry I am new to the command terminal in mac. When I used ps aux | grep mongod, I got the following: shettya 4000 0.0 0.0 2432772 640 s000 S+ 1:11pm 0:00.00 grep mongod , wanted to make sure if this means that mongodb is running ? – nshetty Jul 22 '15 at 12:10

3 Answers3

103

Quick Solution

Run the following in your Terminal:

ps -ef | grep mongod | grep -v grep | wc -l | tr -d ' '

This will get you the number of MongoDB processes running, thus if it is other than 0, then you have MongoDB running on your system.

Step-by-Step

  • The ps -ef | grep mongod part return all the running processes, that have any relation to the supplied string, i.e. mongod, e.g. have the string in the executable path, have the string in the username, etc.

  • When you run the previous command, the grep mongod also becomes a process containing the string mongod in the COMMAND column of ps output, so it will also appear in the output. For that reason you need to eliminate it by piping grep -v grep, which filters all the lines from the input that contain the string grep.

  • So now you have all possible lines that contain string mongod and are not the instances of grep. What to do? Count them, and do that with wc -l.

  • wc -l output contains additional formatting, i.e. spaces, so just for the sake of the beauty, run tr -d ' ' to remove the redundant spaces.

As a result you will get a single number, representing the number of processes you grep'ed for.

bagrat
  • 7,158
  • 6
  • 29
  • 47
  • Thank you for your answer. However, I ran the command and received a 14 (fourteen) altough there are no mongods running. Why? – Pille Dec 02 '15 at 15:19
  • Thats interesting, you might have another processes running, that have `mongod` in command path. Can you run `ps -ef | grep mongod` and post the output? – bagrat Dec 03 '15 at 00:19
  • 1
    @bagrat can we use `pgrep mongod` this command to check that is mongodb is running or not? – gourav Jan 30 '19 at 08:24
31

The answers combining ps and grep should always get you what you need. However, if you have a standard installation that comes with the mongo shell, an easier to remember method is to start the mongo shell

mongo

The shell will give you status of mongodb.

lastoneisbearfood
  • 3,955
  • 5
  • 26
  • 25
0

I had the same problem sometime ago. Using ps -ef (the process status command) will show if the mongod or the mongodb daemon is running. But if it is not running, it will yield a 0 as the status indicating mondodb is not there.

However, if you want to check the installation via the system search on Mac, say using phrase such as mongod or mongodb, it will show the physical location of the installed directory and files starting with the searched phrase. If you get hold of this directory, you will see a bin directory, which will have binary files.

Balepur
  • 138
  • 1
  • 7