[SOLVED] BASH If service runtime statement

Issue

I’m missing the mark on this and I’m just not getting why. I writing a troubleshooter for a custom logstash (But could be any service) deployment we are doing.

The intent:
Check to see if the service has been running longer than 1 minute if has then
Report good then move on to other checks I’m writing
-If not running more than 1-minute wait until 60 seconds on service time and then do checks.
-If service uptime starts over or service is in failed status post error.

SERVICE=logstash
SERVICE_START_TIME=`systemctl status $SERVICE | awk '/Active: active/{print $6" "$7}'`
SERVICE_UP_TIME=$(($(date +%s) - $(date -d "$SERVICE_START_TIME" +%s)))


if systemctl is-active logstash --quiet = active && $SERVICE_UP_TIME > 60s; then
 echo "Logstash is Operating as expected"
elif systemctl is-active logstash = active; then
 echo "Waiting on Logstash to run more than 1 minutes"
wait 60s
else
 echo "Service is NOT Running"
fi

I can get $SERVICE_UP_TIME to return a number but I can’t make it validate against 60 seconds (60s).

Thoughts, I feel I’m doing this the hard way.

Solution

There’s no = argument to systemctl. With the --quiet option it just sets its exit status, there’s nothing to compare with active.

To compare numbers, you need to use [ expression ]. You also shouldn’t have s after 60, and you have to use the -gt option; > is output redirection.

if systemctl is-active logstash --quiet && [ "$SERVICE_UP_TIME" -gt 60 ]; then
    echo "Logstash is Operating as expected"
elif systemctl is-active logstash --quiet; then
    echo "Waiting on Logstash to run more than 1 minutes"
    sleep 60
else
    echo "Service is NOT Running"
fi

Answered By – Barmar

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.