ES 2.2 Use WHM/cPanel chkserv with elasticsearch

PASS

Well-known member
Users of WHM/cPanel can utilize the chkservd monitoring service to detect if Elasticsearch is not running and to restart it automatically. Here's my method: I use AlmaLinux 8 with Elasticsearch v8.16, which should be similar for Rocky Linux as well.

After installing or upgrading Elasticsearch, ensure that it is set to run as a service. This guarantees that Elasticsearch will automatically start whenever you reboot your server manually.

sudo systemctl enable elasticsearch.service



Edit the /etc/chkserv.d/chkservd.conf file. Add...

elasticsearch:1



Create a file called elasticsearch in the /etc/chkserv.d directory.

sudo touch /etc/chkserv.d/elasticsearch

Add the following:

service[elasticsearch]=9200,curl -XGET,HTTP/1.0 400 Ba,systemctl start elasticsearch



Restart chkserv

sudo /scripts/restartsrv_chkservd

Enable elasticsearch monitoring in the WHM service manager (check both boxes)

2024-11-14_19-40-54.webp



Done!


I manually stopped the service to test it.

2024-11-14_18-54-37.webp


The service resumes instantly when chkserv detects it down, but the notification email confirming the service is back up is typically received about 5 minutes later.


2024-11-14_18-54-57.webp
 
Last edited:
UPDATE

Although the method above works, it causes Elasticsearch to restart and Service Manager to hang whenever you use the save button in Service Manager.

A better way to do this is to create a file called restartsrv_elasticsearch and put it here:
/usr/local/cpanel/scripts/restartsrv_elasticsearch

Inside that file put this script:

Code:
#!/bin/bash

# Elasticsearch service name
SERVICE="elasticsearch"
PORT=9200

# Function to check if Elasticsearch is running on the specified port
check_service() {
  if curl -s http://localhost:$PORT >/dev/null; then
    echo "$SERVICE is running on port $PORT."
    return 0
  else
    echo "$SERVICE is not running on port $PORT."
    return 1
  fi
}

# Function to restart the service
restart_service() {
  echo "Restarting $SERVICE..."
  /usr/bin/systemctl restart $SERVICE

  if [ $? -eq 0 ]; then
    echo "$SERVICE restarted successfully."
  else
    echo "Failed to restart $SERVICE."
    exit 1
  fi
}

# Check the service and restart if necessary
check_service || restart_service

CHMOD the restartsrv_elasticsearch file with 755 permissions.

Go back to the elasticsearch service file in the /etc/chkserv.d directory and update with this:

Code:
service[elasticsearch]=9200,curl -XGET,HTTP/1.0 400 Ba,/usr/local/cpanel/scripts/restartsrv_elasticsearch
Note: The service file should have 644 permissions.

You can test everything by manually stopping elasticsearch

Code:
systemctl stop elasticsearch

chkserv runs approximately every five minutes, but if you prefer not to wait, you can manually restart the service to initiate its check.

Code:
/scripts/restartsrv_chkservd

You should receive an email like this:

2024-11-18_00-47-44.webp


Now verify that elasticsearch was restarted successfully by running the following cmd.

Code:
systemctl status elasticsearch

Finally, you can wait 5 mins or restart chkserv again, to get the elasticsearch has recovered email.

2024-11-18_00-49-27.webp

That's it! I hope this help someone.
 
Last edited:
hello,
yes its a little amazing, but....what does it mean? im a little stuck on this part:
This guarantees that Elasticsearch will automatically start whenever you reboot your server manually.
surely it doesnt need to be manually started as default every time the machine is rebooted right?
feel like im missing something...what is the default behaviour?

kind regards
 
surely it doesnt need to be manually started as default every time the machine is rebooted right?
feel like im missing something...what is the default behaviour?
When installing Elasticsearch, it is necessary to enable it as a service to ensure it starts upon server reboot. This step is included in the installation instructions on the Elasticsearch website.

2024-11-18_01-10-11.webp


The instructions I am providing is if the elasticsearch service stops for any reason while the server is running, it will automatically restart.
 
Last edited:
thank you for that, i think im trying to understand why or how it might not be running at times....i had to check because i know that ive done some upgrades and restarts since installing ES....it almost sounds suggesting like if this solution wasnt enacted, it would just stay down indefinately?
sure enough its up and running so something must be starting it somehow or im still missing something...

it seems good to be sure its always running, im trying to understand better which scenario it wouldnt be tho...then for how long it could be down etc...
alma9 is the only diff here i noticed...

i think i will just do it anyway but i am trying to understand some better ;P

regards
 
For me, the service stopped after some cPanel and CSF updates occurred. I wasn't aware until I logged into the AdminCP and saw all the Elasticsearch "unable to connect" errors. This has probably only happened a handful of times over the years. With the rising cost of cPanel, I thought it might be worthwhile to use its monitoring service script. Make me feel that I'm getting my money's worth. :cool: It's already monitoring all the other services, why not that one too.
 
Back
Top Bottom