Check if a process is running, and execute it if not

#!/bin/bash
# filename: process.sh

PROCESSES=1;
x=0

while [ "$x" -lt "$PROCESSES" ];
do
        PROCESS_COUNT=`pgrep -f [ process name ] | wc -l`
        if [ $PROCESS_COUNT -ge $PROCESSES ]; then
                exit 0
        fi
        x=`expr $x + 1`
        [ execute command ] &
done
exit 0

 

Example, if we want to check if mariadb is running or not.

#!/bin/bash
# filename: process.sh

PROCESSES=1;
x=0

while [ "$x" -lt "$PROCESSES" ];
do
        PROCESS_COUNT=`pgrep -f mariadb | wc -l`
        if [ $PROCESS_COUNT -ge $PROCESSES ]; then
                exit 0
        fi
        x=`expr $x + 1`
        systemctl start mariadb &
done
exit 0

 

Top