#!/bin/sh

process_stop(){
    local name="$1"
    local pid=$(pgrep ${name})
    local timeout_sec="$2"
    local wait=0

    if [ -z "$pid" ]; then
        logger "Process $name - $pid not found. Nothing to stop..."
    else
        logger "Sending SIGTERM to process $name - $pid..."
        kill "$pid"

        # Wait for the process to terminate (with timeout)
        while [ $wait -lt $timeout_sec ] && ps -p "$pid" > /dev/null; do
            sleep 1
            wait=$((wait+1))
        done

        # Check if the process is still running
        if ps -p "$pid" > /dev/null; then
            logger "Process $name - $pid did not terminate within $timeout_sec seconds. Sending SIGKILL..."
            kill -9 "$pid"
        else
            logger "Process $name - $pid stopped successfully."
        fi
    fi
}

process_stop syslog-ng 5
