Obfuscates
Linux

Linux | BASH | Checking Services

This week the opportunity to resolve issues with code presented itself. The code was an elaborate bash script that wrote a lot of file and installed some application. There were a couple sections where the code wasn’t written to wait or check on services to validate they were active or even existed. With a little bit of research the two types of checks were made.

Check if service is running, if so, disable it

There are some services enable on linux servers that you may not want running. It’s something the can be checked when running initial install scripts and fairly easy to work out. Below is a sample of what worked in my scenario:

#!/bin/bash 

STATUS=$(systemctl is-active chronyd) #gets the current status of the given service 
if [ "${STATUS}" = "active" ]; then
  echo "The chronyd service is active, disabling it now." 
  systemctl disable --now chronyc
else 
  echo "The service is inactive, nothing to do here." 
fi 

The chronyd service is the example, in reality there were other services.

Check to see if a service started

This next script is going to check to see if a service enters the running state 3 times over 30 seconds. The 4th time it will output an error with a command to run to check on things. This is for the installation of RKE2. On first start, it can take a bit to get going. Also, if you only enable and start the service via script, there’s no insurance that it’s running before you move on to other components. Below is code that seemed to work for the use case.

#!/bin/bash
echo "Waiting for the RKE2 service to start completely"
echo "assigning time var\n"
((TIME=0))
until [ ${TIME} -eq 4 ];
  do
    echo "in the do loop, right befor the status check"
    STATUS=$(systemctl show rke2-server -p SubState --value)
    echo "The status is " $STATUS
    if [ "${STATUS}" != "running" ]; then
      if [ ${TIME} -eq 3 ]; then
        echo "There may be an issue preventing RKE2 from starting. Check the logs via journal. 'journalctl -U rke2-server -xe'\n"
        echo "The script will now halt."
        exit 2
      fi 
      ((TIME=${TIME}+1))
      echo "Time is " $TIME 
      echo "Sleeping for 10 seconds."
      sleep 10
    else
      echo "The server has started successfully" 
      ((TIME=${TIME}+4))
      echo "The time is %v", $TIME
    fi
  done

The couple extra echo statements are used for debugging, they can removed to clean things up a bit. However, this did the trick.