Subtly open Activity Monitor for suspicious CPU hogs
Alec Jacobson
October 13, 2010
As a first attempt at a subtle HCI program, I've written a bash script for Mac that when installed as a cronjob will monitor the running applications and subtly open up the Activity Monitor.app if a process has been hogging the CPU for an extended amount of time.
Here's the bash code I save in monitor-top-process.sh:
#!/bin/bash
record_file=".monitor-top-process.dat"
MAX_COUNT="3"
# make sure record file exists
touch $record_file
# get process with over 70% cpu usage
top_process=`top -l 2 -n 1 -o cpu | \
grep "^ *[0-9]\+" | \
grep -o "[A-z0-9]\+ \+[7-9][0-9.]\+%" | \
sed -e "s/ *[0-9\.]*%//g"`
if [ -n "$top_process" ]; then
old_top_process=`cat $record_file | \
grep -o "top_process=.*" | \
sed -e "s/top_process=//g"`
if [ "$top_process" = "$old_top_process" ]; then
count=`cat $record_file | \
grep -o "count=.*" | \
sed -e "s/count=//g"`
count=`expr "$count" + 1`
if [ "$count" -ge "$MAX_COUNT" ]; then
activity_monitor_running=`top -l 1 | grep Activity`
if [ -z "$activity_monitor_running" ]; then
open "/Applications/Utilities/Activity Monitor.app/"
fi
fi
else
count="1"
fi
echo -e "top_process=$top_process\ncount=$count" > $record_file
else
# no top process clear record
echo "" > $record_file
fi
Then I issue:
crontab -e
and add the following to run this script every five minutes:
*/5 * * * * bash [path to]/monitor-top-process.sh
It's not the prettiest way to do it, but a quick proof of concept.