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.
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 isstdout
, 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?pgrep -ax mongod
571 /usr/bin/mongod --config /etc/mongodb.conf