Created
July 30, 2018 09:15
-
-
Save zanglang/46bbb3e61d213c27d0dbe887c1a5d6b2 to your computer and use it in GitHub Desktop.
Bash script to check for Java processes that have lived longer than their maximum expected run time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/sh | |
# number of ticks since boot | |
UPTIME=$(awk '{print $1}' /proc/uptime | xargs printf '%.0f') | |
WARNINGS= | |
FOUND=0 | |
for pid in $(pgrep -f java) | |
do | |
# get elapsed jiffies in seconds for each java process | |
STAT=$(cat /proc/$pid/stat) | |
STARTTIME=$(getconf CLK_TCK | xargs expr $(awk '{print $22}' /proc/$pid/stat) /) | |
TIME=$(expr $UPTIME - $STARTTIME) | |
if [ "$(expr $UPTIME - $STARTTIME)" -gt 7200 ] # 2 hours | |
then | |
WARNINGS="$WARNINGS\nPID $pid running since $(ps h -p $pid -o lstart)" | |
((FOUND++)) | |
fi | |
done | |
if [ "$FOUND" -gt 0 ] | |
then | |
echo -e "WARNING: $FOUND possibly stuck java processes.\n$WARNINGS" | |
exit 1 | |
fi | |
echo "OK: no stuck java processes." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment