Keeping the ElasticSearch service running on an Ubuntu server

A local ElasticSearch instance powers the Chinese example sentence search feature on Chinese Boost. This works fine, but the ElasticSearch instance has a habit of crashing about once a day.

While I’ve been investigating why it crashes and how to prevent that from happening, I also wanted to have the EC2 Ubuntu server it runs on restart the ElasticSearch service automatically as a workaround.

This can be done using systemd.

You can add extra config for the elasticsearch service with this command:

EDITOR=vim sudo systemctl edit elasticsearch.service

In the editor, add this and save:

[Service]
Restart=always

Then reload the systemd daemon to get the updated config:

sudo systemctl daemon-reload

Finally, cat the elasticsearch service config to check that it has picked up the new restart behaviour:

sudo systemctl cat elasticsearch.service

You should see the additional config at the end:

# /etc/systemd/system/elasticsearch.service.d/override.conf
[Service]
Restart=always

Tech mentioned