# kill runaway processes at 100% CPU usage (Mac OSX)
export PNAME=$1
if [[ ! $PNAME ]]
then
echo "usage: runaways.sh [process name]"
exit
fi
export PPERCENT='100.0'
SECONDS_TO_TEST=20
THRESHOLD=15
SECONDS_RUNAWAY=$(top -l $SECONDS_TO_TEST | perl -lne'BEGIN{$c=0};/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && do { $c++ };END{print$c}');
if [[ $SECONDS_RUNAWAY -gt $THRESHOLD ]]
then
echo "killing $PNAME at $PPERCENT"%...
PID=$(top -o cpu -l 2 | perl -lne'/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && print $1')
if [[ $PID ]]
then
echo "Killing $PNAME"
kill -9 $PID
else
echo "$PNAME does not exceed the threshold for a runaway"fi
else
echo "$PNAME is not runaway"
fi