Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-restart Mongodb in Ubuntu 16.04

I can't figure out how to auto-restart mongodb on Ubuntu 16.04 in the event that it crashes due to memory-pressure.

I'm using supervisord for other scripts etc, but mongod is through systemctl and I'm not sure how that ties into it.

like image 427
Dev Sanghani Avatar asked Apr 11 '18 06:04

Dev Sanghani


People also ask

How do I start MongoDB automatically?

You can either put the command in your /etc/bashrc script under and if condition i.e. if the mongod process is not already running, then start it. Other way is to modify your /etc/rc. local and add the command to start mongod in that file. It will start at bootup.


2 Answers

Figured it out:

  1. Edit your mongod service: sudo vim /lib/systemd/system/mongod.service
  2. Add Restart=always under service
  3. Reload systemctl daemon: sudo systemctl daemon-reload

Now whenever mongod gets killed. It'll get respawned by systemctl.

like image 145
Dev Sanghani Avatar answered Sep 28 '22 08:09

Dev Sanghani


When you install mongoDB just run systemctl enable mongod.service on terminal.

This will make your mongoDB service auto-start on every restart.

like image 39
Muhammad Usman Avatar answered Sep 28 '22 08:09

Muhammad Usman