Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created December 1, 2014 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save komasaru/b2d309df2e7da7702fc0 to your computer and use it in GitHub Desktop.
Save komasaru/b2d309df2e7da7702fc0 to your computer and use it in GitHub Desktop.
Bash script to check processes.
#!/bin/bash
# ------------------------------------------------------------
# Bash script to check processes.
# ------------------------------------------------------------
# RETURN CODE
# 0: processes are not running.
# 1: one process is running. (CPU time < configured value)
# 2: one process is running. (CPU time >= configured value)
# 3: more than two processes are running.
# ------------------------------------------------------------
#
PS_NAME="<process_name>"
CPU_MAX=000100 # Format: `HHMMSS`
# Process counts
CNT=`ps -ef | grep -e "$PS_NAME" | grep -v grep | wc -l`
if [ $CNT -eq 0 ]; then
echo "[RET-CODE:0] processes are not running."
exit 0
elif [ $CNT -eq 1 ]; then
# CPU time
TM=`ps -ef | grep -e "$PS_NAME" | grep -v grep | awk '{ print $7 }'`
TM=${TM:0:2}${TM:3:2}${TM:6:2}
if [ $TM -lt $CPU_MAX ]; then
echo "[RET-CODE:1] one process is running. (CPU time < configured value)"
exit 1
else
echo "[RET-CODE:2] one process is running. (CPU time >= configured value)"
exit 2
fi
else
echo "[RET-CODE:3] more than two processes are running."
exit 3
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment