#!/bin/bash
# Filename: Process_Monitor.sh
# Auth: shuhui

### Define Variables

function monitor() {
    pgrep -x $1 >/dev/null 2>&1
    return $?
}

if monitor "sshd"
then 
    status="up"
else
    status="down"
fi

while [[ true ]]
do
    monitor "sshd"
    rc=$?

    if [[ $rc -eq 0 && $status = "up" ]]
    then
        status="up"
        logger -t "monitor" "sshd is up."
    fi

    if [[ $rc -ne 0 && $status = "down" ]]
    then
        status="down"
        logger -t "monitor" "sshd is down."
    fi

    sleep 5
done
