BudiBadu Logo
Samplebadu

Bash by Example: Process Monitor Script

Bash 5.0+

Continuously checking if a process is running and logging its status by monitoring process IDs with pgrep or ps commands, implementing process health checks for critical services, recording uptime and downtime events, alerting on process failures, and maintaining audit trails of process lifecycle events.

Code

#!/bin/bash

PROCESS_NAME="nginx"
LOG_FILE="monitor.log"

echo "Monitoring $PROCESS_NAME (Ctrl+C to stop)..."

while true; do
    # pgrep returns 0 if process found, 1 if not
    if pgrep -x "$PROCESS_NAME" > /dev/null; then
        echo "$(date): $PROCESS_NAME is RUNNING."
    else
        echo "$(date): ALERT! $PROCESS_NAME is NOT running."
        # echo "Alert" | mail -s "Down" [email protected]
    fi
    
    # Wait 5 seconds before next check
    sleep 5
done

Explanation

In a production environment, ensuring that critical services (like web servers, databases, or application backends) are running is paramount. A process monitor script acts as a watchdog. It runs in an infinite loop, periodically checking for the existence of a specific process and taking action if it disappears.

The pgrep command is the tool of choice here. Unlike ps | grep, which is prone to false positives (matching the grep command itself), pgrep is designed specifically to look up process IDs by name. The -x flag ensures an exact match, so searching for "java" won't accidentally match "javascript".

This script demonstrates the basic logic of a watchdog. In a real-world scenario, the "else" block would do more than just print to the screen; it would likely send an email alert via mail or sendmail, or even attempt to restart the service automatically using systemctl restart.

Code Breakdown

10
pgrep -x looks for a process where the name matches exactly. If you used just pgrep nginx, it might match nginx-helper or similar, which could lead to false confidence.
18
The sleep command is critical in infinite loops. Without it, the script would execute thousands of times per second, spiking CPU usage to 100% and potentially freezing the system.