#!/bin/bash 
#
# pcheck.stackx.sh: process checker by ScalarX Technology
# Version: 1.0 (stable for use in Production)
# Author: Christophe Casalegno / Brain 0verride
# Website: https://scalarx.com
# Twitter: https://twitter.com/ScalarTech
# Email: christophe.casalegno@scalarx.com
# Note: 
#
# ./pcheck.stackx.sh nomduprocess timebeforerecheckinsec

RED='\033[38;5;160m' #ex echo -e "${RED} ALERT"
NC='\033[0m'	 	 #ex echo -e "${NC} Normal"
GREEN='\033[38;1;32m' 	 #ex echo -e "${GREEN} OK"
YELLOW='\033[38;5;226m' #ex echo -e "${YELLOW} Warning"

pcs="$1" # Name of the process to check
sbc="$2" # Time to sleep before recheck

if [ -z "$pcs" ]

then

	echo -e "${RED} [ ERROR ]" "${NC}  You must specify the process to monitor"
	exit 0
	
fi

if [ -z "$sbc" ]

then

	echo -e "${RED} [ ERROR ]" "${NC}  You must specify the time in seconds between each check"
	exit 0
	
fi

# Function to check if the process is running

check_process() {
    echo "$tstamp : checking $1"
    [ "$1" = "" ]  && return 0
    [ "$(pgrep -n -x "$1")" ] && return 1 || return 0
      }

while true ; do 
tstamp=$(date +%T) #Define timestamp

# Execute checking

echo "$tstamp : checking if the process running..."
check_process "$pcs"

# If process don't running, alert support 

[ $? -eq 0 ] && echo "$tstamp: $pcs ERROR"
sleep "$sbc"

done
