I am running MongoDB on an Ubuntu server. It's using an upstart script to start mongod when the machine boots. I noticed that if the process crashes, it doesn't get restarted.
How can I make sure that if it crashes, the mongod process is restarted?
I am running MongoDB on an Ubuntu server. It's using an upstart script to start mongod when the machine boots. I noticed that if the process crashes, it doesn't get restarted.
How can I make sure that if it crashes, the mongod process is restarted?
I found the answer myself: the simplest way to achieve that is to add the following two lines at the end of the upstart script installed by MongoDB (/etc/init/mongodb.conf
):
respawn
respawn limit 10 90
This will try to restart the process if it terminates, and stop if it crashes more than 10 times in 90 seconds.
I don't think newer versions of MongoDB/Ubuntu use upstart. Here's how I achieved this on Ubuntu 18.04, MongoDB 4.0.2:
Open /lib/systemd/system/mongod.service
in a text editor:
sudo nano /lib/systemd/system/mongod.service
Add Restart=on-failure
and RestartSec=10
to restart the server after 10 seconds whenever it exits with a non-zero exit code:
[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
Restart=on-failure
RestartSec=10
...
Use Restart=always
if you'd like to restart no matter the exit code.
Then you can systemctl daemon-reload
and then restart the service, or just reboot the server.
Building on the accepted answer (and since I do not have enough rep to comment on it)
Respawn
is NOT a mongo configuration parameter, it is for the upstart service configuration file.
respawn
respawn limit 10 90
Digital Ocean has a nice tutorial for using upstart and systemd (newer). https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples
Open mongod.service
by this systemctl command:
sudo systemctl edit --full mongod.service
edit the [service]
block by just add these two commands:
Restart=on-failure
RestartSec=10
and restart the service
sudo systemctl restart mongod.service
OR, check the status, again
sudo systemctl status mongod.service
YOU CAN USE THIS TECHNIQUE IN ANY SERVICE TO RESTART ON FAILED
@syedasadrazadevops
I think you want something like Monit or God here to automatically restart services when they stop/crash. Your description of the crash itself would suggest the OOM Killer, which can be avoided by configuring some swap space on the host.
First of all, you should understand why the system service crashes. Lack of memory? Bugged libs? Wrong or not updated package? Your software using MongoDB is not working properly?
In the unlucky case you can't reach an answer, you could brute force your system: I'd check regularly via a script if the mongod PID file exists, it should be stored somewhere in /var/run, if not, service mongod start
.
You could use Supervisord, this tool can restart anything you need and it is very simple to configure.