|
1 | | -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
| 2 | +############################################################################### |
| 3 | +# Script Name : linux_check_processes |
| 4 | +# Description : Check if one or more processes are running. |
| 5 | +# |
| 6 | +# Exit Codes : |
| 7 | +# 0 - OK (All processes running) |
| 8 | +# 1 - UNKNOWN (Invalid input / script failure) |
| 9 | +# 2 - WARNING (One or more processes not running) |
| 10 | +# |
| 11 | +# Usage : |
| 12 | +# --process=proc1,proc2,...,procN |
| 13 | +# |
| 14 | +# Examples : |
| 15 | +# ./linux_check_processes --process=sshd |
| 16 | +# ./linux_check_processes --process=sshd,crond,nginx |
| 17 | +# PROCESSES="sshd crond" ./linux_check_processes |
| 18 | +# |
| 19 | +# Repository : https://github.com/amidaware/community-scripts |
| 20 | +# Category : TRMM (nix):System Monitoring |
| 21 | +# Version : 1.0 |
| 22 | +# Last Updated: 2026-02-09 |
| 23 | +############################################################################### |
2 | 24 |
|
3 | | -# Checks if one or more processes are running. |
4 | | -# The env variable PROCESSES must be passed in the script using format PROCESSES=process1 process2 process3 |
| 25 | +PROCESSES_LIST=() |
5 | 26 |
|
| 27 | +# ----------------------------- Arg Parsing --------------------------------- |
6 | 28 |
|
7 | | -if [ -z "$PROCESSES" ]; then |
8 | | - echo "Please specify processes in the environment variable PROCESSES using the format PROCESSES=process1 process2 process3" |
| 29 | +while [[ $# -gt 0 ]]; do |
| 30 | + case "$1" in |
| 31 | + --process=*) |
| 32 | + process_arg="${1#*=}" |
| 33 | + |
| 34 | + [[ -z "$process_arg" ]] && { |
| 35 | + echo "ERROR: --process requires a value" |
| 36 | + exit 1 |
| 37 | + } |
| 38 | + |
| 39 | + IFS=',' read -r -a processes <<< "$process_arg" |
| 40 | + |
| 41 | + for proc in "${processes[@]}"; do |
| 42 | + [[ -z "$proc" ]] && { |
| 43 | + echo "ERROR: Empty process name in --process list" |
| 44 | + exit 1 |
| 45 | + } |
| 46 | + PROCESSES_LIST+=("$proc") |
| 47 | + done |
| 48 | + shift |
| 49 | + ;; |
| 50 | + *) |
| 51 | + echo "ERROR: Unknown argument: $1" |
| 52 | + exit 1 |
| 53 | + ;; |
| 54 | + esac |
| 55 | +done |
| 56 | + |
| 57 | +# ----------------------- Environment Variable Support ----------------------- |
| 58 | + |
| 59 | +if [[ -n "$PROCESSES" ]]; then |
| 60 | + read -r -a env_procs <<< "$PROCESSES" |
| 61 | + PROCESSES_LIST+=("${env_procs[@]}") |
| 62 | +fi |
| 63 | + |
| 64 | +# ---------------------------- Validation ----------------------------------- |
| 65 | + |
| 66 | +if [[ ${#PROCESSES_LIST[@]} -eq 0 ]]; then |
| 67 | + echo "ERROR: No processes specified" |
9 | 68 | exit 1 |
10 | 69 | fi |
11 | 70 |
|
12 | | -# Loop over the list of processes and check if they are running |
13 | | -for proc in $PROCESSES; do |
14 | | - if pgrep -x "$proc" >/dev/null; then |
15 | | - echo "$proc is running" |
16 | | - else |
17 | | - echo "$proc is not running" |
| 71 | +command -v pgrep >/dev/null 2>&1 || { |
| 72 | + echo "ERROR: Required command 'pgrep' not found" |
18 | 73 | exit 1 |
19 | | - fi |
| 74 | +} |
| 75 | + |
| 76 | +# ----------------------------- Execution ----------------------------------- |
| 77 | + |
| 78 | +exit_code=0 |
| 79 | + |
| 80 | +for proc in "${PROCESSES_LIST[@]}"; do |
| 81 | + if pgrep -x "$proc" >/dev/null 2>&1; then |
| 82 | + echo "OK: Process '$proc' is running" |
| 83 | + else |
| 84 | + echo "WARNING: Process '$proc' is NOT running" |
| 85 | + exit_code=2 |
| 86 | + fi |
20 | 87 | done |
| 88 | + |
| 89 | +exit "$exit_code" |
0 commit comments