#!/usr/bin/env bash ## stuckmeter.sh ## Licensed under GPLv3 by Davis Remmel ## ## This program was written to detect when pdftk gets stuck, but it may ## be useful for other processes too. Use it as such: ## ./stuckmeter.sh pdftk ## to create a file (/tmp/stuckometer-pdftk.pids). The next time this ## program is called, it will compare the previously-collected pids with ## the new ones, and if there exist processes which are the same between ## the two, it will exit with a non-zero code. ## ## Changelog: ## 2018-10-22: Program was written pname="$1" if [[ "" == "$pname" ]] then >&2 echo "No process name given" exit 1 fi # Rotate the pidsfiles oldpidsfile="/tmp/stuckmeter-${pname}.oldpids" newpidsfile="/tmp/stuckmeter-${pname}.newpids" touch "$oldpidsfile" "$newpidsfile" cat "$newpidsfile" > "$oldpidsfile" pgrep "$pname" > "$newpidsfile" # Diff the files matchingpids="$(grep -f "$oldpidsfile" "$newpidsfile" | tr "\n" " ")" if [[ "" != "$matchingpids" ]] then >&2 echo "Stuck $pname processes: $matchingpids" exit 1 fi echo "No stuck $pname processes" exit 0