How to make elasticsearch service autostart

#1

I’ve been using elasticsearch for about a month now and having good results. I noticed that the service doesn’t restart automatically after the system reboots like the solr service used to. How can I change this, so that I don’t have to SSH and restart the service after reboot?

thanks.

#2

Well… depending on your host OS (mine is Debian 9), this is as simple as

systemctl enable elasticsearch

#3

i’m running Ubuntu lts 16.04

I ran sudo systemctl enable elasticsearch.service and tested upon reboot, now the service fails to start.

ava@NextCloud:~$ sudo systemctl start elasticsearch.service
ava@NextCloud:~$ systemctl status elasticsearch.service
â— elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2018-03-05 10:23:34 PST; 967ms ago
     Docs: http://www.elastic.co
  Process: 1685 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=1/FAILURE)
 Main PID: 1685 (code=exited, status=1/FAILURE)

Mar 05 10:23:33 NextCloud systemd[1]: Started Elasticsearch.
Mar 05 10:23:34 NextCloud systemd[1]: elasticsearch.service: Main process exited, code=exited, status=1/FAILURE
Mar 05 10:23:34 NextCloud systemd[1]: elasticsearch.service: Unit entered failed state.
Mar 05 10:23:34 NextCloud systemd[1]: elasticsearch.service: Failed with result 'exit-code'.

to revert should I run systemctl disable elasticsearch.service ?

#4

Why do you try to start it a 2nd time after boot. Just check with

systemctl status elasticsearch.service

after reboot. You can’t run it two times, since the port is already taken by the autostart-app.

#5

I did but it was in failed status, so I attempted to start it.

#6

I just rebooted again to test. the service is now failing to start after reboot:

ava@NextCloud:~$ systemctl status elasticsearch.service
â— elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2018-03-05 10:36:48 PST; 21s ago
     Docs: http://www.elastic.co
  Process: 1100 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=1/FAILURE)
 Main PID: 1100 (code=exited, status=1/FAILURE)

Mar 05 10:36:40 NextCloud systemd[1]: Started Elasticsearch.
Mar 05 10:36:48 NextCloud systemd[1]: elasticsearch.service: Main process exited, code=exited, status=1/FAILURE
Mar 05 10:36:48 NextCloud systemd[1]: elasticsearch.service: Unit entered failed state.
Mar 05 10:36:48 NextCloud systemd[1]: elasticsearch.service: Failed with result 'exit-code'.
ava@NextCloud:~$ service elasticsearch status
â— elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2018-03-05 10:36:48 PST; 35s ago
     Docs: http://www.elastic.co
  Process: 1100 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet (code=exited, status=1/FAILURE)
 Main PID: 1100 (code=exited, status=1/FAILURE)

Mar 05 10:36:40 NextCloud systemd[1]: Started Elasticsearch.
Mar 05 10:36:48 NextCloud systemd[1]: elasticsearch.service: Main process exited, code=exited, status=1/FAILURE
Mar 05 10:36:48 NextCloud systemd[1]: elasticsearch.service: Unit entered failed state.
Mar 05 10:36:48 NextCloud systemd[1]: elasticsearch.service: Failed with result 'exit-code'.
ava@NextCloud:~$
#7

If this is the case, then you will have to take look at the elasticsearch logs. This smells a bit like a permissions issue. If you can manually start elasticsearch using servicectl start elasticsearch, check which user runs elasticsearch afterwards. Then, check the startup script if it also runs as this user.

#8

For now, I have reverted to snapshot so everything is back the way it was beforehand. Elastic search works fine, it just doesn’t start up automatically when the system reboots.

looking through the elastic search logs, there are a couple of warnings involving memory usage, trouble indexing particular files, that sort of thing.

I am unsure of how to check or change what account a service runs under, so I will investigate this next.

#9

I think you may be on to something: /etc/init.d/elasticsearch is owned by root. when looking at it in nano, the first lines state

if [ `id -u` -ne 0 ]; then
        echo "You need root privileges to run this script"
        exit 1
fi

In the log I posted above, you can see it exited with error code 1, indicating this may be the root of the problem. Should I chown the file to www-data?

#10

No, actually this code requires the startscript to be run as root. If you enabled it from another account, you may try this:

sudo systemctl enable elasticsearch.service

or become root and enable elasticsearch.service from the root acccount.