Question: Using the `apt-get` package for ElasticSearh, how can I configure the service to restart itself automatically after crashing on Ubuntu?

Answer: 
Restart on failure option is [missing](https://github.com/elastic/elasticsearch/blob/v5.5.1/distribution/src/main/packaging/systemd/elasticsearch.service) in the default service of elasticsearch.

So, We can add **Restart=always** [option](https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=) in the service.

### Steps to add - Restart=always

1. Edit elasticsearch service unit file using the command `sudo systemctl edit elasticsearch.service`. This command will create a file `/etc/systemd/system/elasticsearch.service.d/override.conf`.
2. Now, add the following lines in the unit file.

```
[Service]
Restart=always

```
3. Save the file and refresh the unit file using command `sudo systemctl daemon-reload`
4. Can check the changes using command `sudo systemctl cat elasticsearch.service`.

### Note:

We can use **Restart=** always, on-abnormal, on-success, on-failure, etc based on the requirement. [Reference](https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=).

Editing unit file - [Reference](https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units#editing-unit-files)
**Auto restart elasticsearch services in 7.14.1:**

Go to:

```
nano /usr/lib/systemd/system/elasticsearch.service 

```

The location of the service file is changed in 7.14.1

Then add this line to the service file:

```
[Service]

Restart=always

```

After that save the file and restart the elasticsearch service.

Now you are good to go. (After this there won't be any crash)