Having a script where I start/stop various services depending on what I need. For example Apache, BIND, MySQL, PostFix, Memcached, Tomcat, ... and so on.

Depending on status options to script etc. various actions are taken.

For most this works fine: (example with Apache)

if service apache2 status >/dev/null; then
    # ... do some action

For others one have to use other tricks like mysqladmin ping.

However I'm not able to find any good way to check status for mongodb.

service mongodb status

It has the same "sickness" as MySQL and yield mongodb stop/waiting and also evaluates to true, even if service is not up and running.

Have looked at mongod, mongostat etc. but not found any option for this.

One way could be to use ps or perhaps better netstat and combine either with grep or the like, but wondering if there is a better (more native/targeted) way to check status. Could also grep for stop/waiting, but not sure how reliable this is, and guess it could likely break in a future upgrade.


What about just using pgrep to find the process, mongod.

$ pgrep mongod
  • Wondered if there was some more direct way like mysqladmin ping, but can't find any. I'll accept this as it is good as any I guess. – user13500 Apr 20 '14 at 17:57

I haven't tried mongodb. But from this answer here, I see ps options as you had mentioned in your quesiton. Also, you had mentioned only service mongodb status. However, you can also try /etc/init.d/mongod status.

check with either:

   ps -edaf | grep mongo   # "ps" flags may differ on your OS


   /etc/init.d/mongod status


   service mongodb status

to see if mongod is running

check the log file /var/log/mongo/mongo.log to see if there are any problems reported

  • /etc/init.d/mongodb status gives same exit as service mongodb status, as do status mongodb. No such file as /etc/init.d/mongod – user13500 Apr 20 '14 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.