How to craft a crontable entry that keeps MongoDB running in the event of a crash?

I need help crafting a crontable entry that will make sure the MongoDB service (daemon) is running on a particular port and if it is not, start it. I want to set this up so that if MongoDB crashes, it is automatically restarted. I'd like to have the check occur every five minutes. I have done this for other server processes but those services automatically exit if they find the port they want to bind to is already in use. My concern as a MongoDB newbie is that I'll end up starting yet another instance of MongoDB every five minutes instead of making sure only one instance is loaded. For example, here is my crontable entry for a chatbot service I keep running:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * cd /home/[username]/ChatScript; ./LinuxChatscript32 2>/home/[username]/cronserver.log 

How can I create something similar for MongoDB?

3 Answers

*/5 * * * * /bin/bash -c 'if ! pgrep mongod; then /usr/sbin/service mongodb start; fi'
2

Take a look at supervisor.

It's supposed to do exactly that.

For anyone who stumbles upon this now, I have found Monit to be most useful for this. it can monitor the port and restart if necessary as well as monitor ram/cpu usage.

The reason I would suggest this is because, in my experience, mongodb can appear to be online with the process still running, but not responding due to high cpu usage or running out of ram.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like