Bash: Continuously check processes running or not with some parameters

Solution 1:

You can try something like this:

#!/bin/bash

service="nginx"
seconds=2
retries=3

until (( retries-- == 0 ))
do
    property=$(systemctl show --property MainPID "$service")

    if [[ $property == MainPID=0 ]]
    then
        echo "$service stopped"
        echo "$service starting"
        systemctl start "$service" >& /dev/null
    else
        echo "$service is running"
        exit
    fi

    sleep "$seconds"
done

echo "$service is broken"