Skip to content

Instantly share code, notes, and snippets.

@VidyasagarMSC
Created May 12, 2025 15:13
Show Gist options
  • Select an option

  • Save VidyasagarMSC/be7213a35e43b27d2db5c7e4fc51ee7d to your computer and use it in GitHub Desktop.

Select an option

Save VidyasagarMSC/be7213a35e43b27d2db5c7e4fc51ee7d to your computer and use it in GitHub Desktop.
#!/bin/bash
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Initialize variables
JSON_OUTPUT=false
LOG_FILE=""
SILENT=false
PROCESS_LIST=()
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--json) JSON_OUTPUT=true ;;
--log-file) LOG_FILE="$2"; shift ;;
--silent) SILENT=true ;;
*) PROCESS_LIST+=("$1") ;;
esac
shift
done
# Show usage if no processes specified
if [[ "${#PROCESS_LIST[@]}" -eq 0 ]]; then
echo "Usage: $0 [OPTIONS] <process1> <process2> ..."
echo "Options:"
echo " --json Output in JSON format"
echo " --log-file FILE Log results to a file"
echo " --silent Suppress terminal output"
exit 1
fi
# Get system information
if [[ "$(uname)" == "Darwin" ]]; then
# macOS detection
DISTRO="macOS $(sw_vers -productVersion)"
PS_CMD="ps -p %s -o pid=,user=,command="
else
# Linux detection
DISTRO=$(grep '^NAME=' /etc/os-release | cut -d'"' -f2 2>/dev/null || uname -s)
PS_CMD="ps -p %s -o pid=,user=,cmd= --no-headers 2>/dev/null || ps -p %s -o pid=,user=,command= --no-headers"
fi
HOSTNAME=$(hostname)
TIMESTAMP=$(date +'%Y-%m-%d %H:%M:%S')
# Initialize JSON output if enabled
if $JSON_OUTPUT; then
JSON_RESULT="{ \"hostname\": \"$HOSTNAME\", \"timestamp\": \"$TIMESTAMP\", \"os\": \"$DISTRO\", \"processes\": ["
fi
SCRIPT_PID=$$
SCRIPT_NAME=$(basename "$0")
# Function to get process information (cross-platform)
get_process_info() {
local pid="$1"
if [[ "$(uname)" == "Darwin" ]]; then
# macOS ps format
ps -p "$pid" -o pid=,user=,command= | awk '{$1=$1;print}'
else
# Linux ps format
eval $(printf "$PS_CMD" "$pid" "$pid")
fi
}
# Main process checking loop
for pattern in "${PROCESS_LIST[@]}"; do
# Find matching processes excluding this script
if [[ "$(uname)" == "Darwin" ]]; then
# macOS pgrep syntax
pids=$(pgrep -f "$pattern" | grep -vw "$SCRIPT_PID")
else
# Linux pgrep syntax
pids=$(pgrep -f "$pattern" | grep -vw "$SCRIPT_PID")
fi
if [[ -n "$pids" ]]; then
status="RUNNING"
color="$GREEN"
details=""
while read -r pid; do
process_info=$(get_process_info "$pid")
details+="$process_info\n"
done <<< "$pids"
else
status="NOT RUNNING"
color="$RED"
details="No matching process found"
fi
# Terminal output (unless silent mode)
if ! $SILENT; then
if ! $JSON_OUTPUT; then
printf "%-25s ${color}%-15s${NC} %s\n" "$pattern" "$status" "$(echo -e "$details" | head -n 1)"
[[ $(echo -e "$details" | wc -l) -gt 1 ]] && echo -e "$details" | tail -n +2
echo "----------------------------------------------------------------"
fi
fi
# JSON output (if enabled)
if $JSON_OUTPUT; then
if [[ -n "$pids" ]]; then
while read -r pid; do
process_info=$(get_process_info "$pid")
pid_num=$(echo "$process_info" | awk '{print $1}')
user=$(echo "$process_info" | awk '{print $2}')
command=$(echo "$process_info" | awk '{$1=$2=""; print $0}' | sed 's/^ //')
JSON_RESULT+="{ \"process\": \"$pattern\", \"status\": \"$status\", \"pid\": \"$pid_num\", \"user\": \"$user\", \"command\": \"$command\" },"
done <<< "$pids"
else
JSON_RESULT+="{ \"process\": \"$pattern\", \"status\": \"$status\", \"pid\": null, \"user\": null, \"command\": null },"
fi
fi
# Log to file (if enabled)
if [[ -n "$LOG_FILE" ]]; then
echo -e "[$TIMESTAMP] $pattern - $status - $(echo -e "$details" | tr '\n' ' ')" >> "$LOG_FILE"
fi
done
# Finalize JSON output
if $JSON_OUTPUT; then
JSON_RESULT=${JSON_RESULT%,} # Remove trailing comma
JSON_RESULT+=" ] }"
if command -v jq >/dev/null 2>&1; then
echo "$JSON_RESULT" | jq '.' 2>/dev/null
else
echo "$JSON_RESULT"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment