0

I'm using mongodb database to save my application data into collection but by some reasons the mongodb connection is break and there is lot of problems occurs and I want to check the mongodb connection through terminal command. I searched for it and I found three commands which will be used but I'm confused that which command I have to use in the golang code.

Command 1. pgrep mongod this command will return me PID (process Id).

Command 2. ps -ef | grep mongod this command will returns me mongodb 15678 1 1 13:58 ? 00:00:01 /usr/bin/mongod --config /etc/mongod.conf

Command 3. ps -ef | grep mongod | grep -v grep | wc -l | tr -d ' ' this command I found from the stackoverflow accepted answer and it may be returns 0, 1. if there is other value except 0 then your mongodb is running as the answer say you can see link of the question

link:- https://stackoverflow.com/questions/31561098/how-to-check-if-mongo-db-is-running-on-mac

Command 4. service --status-all | grep mongod will show me the mongod service with a signed like [ + ], or [ - ]. In the documentation they says that if the service with [ + ] the service is running and [ - ] the service is stopped. But on my localhost the mongod is running and it will show me [ - ] mongod when I execute this command. Why?

link:- https://www.rosehosting.com/blog/how-to-list-all-services-in-linux/

Please tell me that which Command I have to used to check that the mongodb is running or not I have to use that command in my golang code.

  • I think if you just want to check the service is running, you can do just use systemctl status mongod.service and then check the return code (though the name of the serivice you may have to change, i don't remember exactly what it is – j-money Jan 30 '19 at 9:33
  • @j-money Can we use this command for the code purpose will it return the status? – gourav Jan 30 '19 at 10:12
  • I'm not sure I understand your question, it will print info about the service to stdout, but when a service isn't active it will give back a non-zero return value. Are you writing a bash script? Or in another language? – j-money Jan 30 '19 at 11:23
  • pgrep -ax mongod 571 /usr/bin/mongod --config /etc/mongodb.conf – user216043 Jan 30 '19 at 11:29
0

You could run something along the lines of

if pgrep -x mongodd
 then echo "ok"
 else echo "not ok"
fi
| improve this answer | |
  • please check this link is this commands are right to do this – gourav Jan 30 '19 at 12:49

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.