Tuesday, July 31, 2018

Start mongodb automatically after it gets crashed or down.


Start mongodb automatically after it gets crashed or down.
Recently we have started getting an issue where on one of our server we are running both Redis and Mongodb. When we have memory issue Redis is killing Mongod process on Linux box.

One of the best possible solutions until we separate mongodb and Redis server is we have to start Mongodb service automatically after it crash. Did lots of research on internet could not find direct and easy solution but most of them helped me to find right solution. Writing this article by hoping this might help someone.

Environment:
We are running on Mongodb on RHEL 7 which uses systemctl for handling all services and below details applies to same.

First we need to find the service file location which can be found as below:
Sudo systemctl status mongod.service

Check for “loaded” line to find the service location.

Example: Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)

Edit the file mongod.service using your favorite editor and add below two lines:
Restart=on-failure
RestartSec=5s
Restart on failure will help restart mognodb service automatically and restartsec parameter will allow services to wait for 5 seconds in this case. Completed reference for can be found on link:

https://www.freedesktop.org/software/systemd/man/systemd.service.html

Post this we need to reload units with the help of below command:
sudo systemctl daemon-reload

Once done restart your mongodb service with the help of below commands:
sudo systemctl stop mongod.service
sudo systemctl start mongod.service

For testing you can kill the mongodb service with the help of below commands:

ps -ef | grep mongod.service
Will get you PID number.

sudo kill -9 process_number
Will kill mongodb PID.
Wait for 5 seconds you will see mongodb back online again.

All the very best Thank you, Nag.

No comments:

Post a Comment

Configure a Linux Service to Start Automatically After a Crash or Reboot

Configure a Linux Service to Start Automatically After a Crash or Reboot Below articles defines how to start a service automatically after ...