Is there an app that will notify me (in notifications center) if a process is using up a high percentage of CPU for a specified amount of time?

The other day, I was sitting in a coffee shop working, when I looked down to find that 25% of my battery was left.

I looked at my Activity Monitor and saw that the time machine process was going at 90% for a while (as long as I was watching it).

Are there any apps that can notify me if a process is at a certain level for a certain amount of time?


Solution 1:

You could run a script like this every minute with cron or launchd:

#!/bin/bash

cpulimit=50
prefix=${TMPDIR}cron_cpu
current=$(ps -erco %cpu,command | tail -n+2 | sed 's/^ *//')
echo "$current" > $prefix$(date +%s)
a=($prefix*); for ((i=0;i<=${#a[@]}-3;i++)); do rm "${a[i]}"; done
[[ $(awk '{s+=$1}END{printf "%i",s}' <<< "$current") -lt $cpulimit ]] && exit
averages=$(awk '{cpu=$1;sub(/[^ ]+ /,"");a[$0]+=cpu;c[$0]++}END{for(i in a){printf "%.1f %s\n",a[i]/c[$0],i}}' $prefix* | sort -rn)
if [[ $(awk '{s+=$1}END{printf "%i",s}' <<< "$averages") -ge $cpulimit ]]; then
    terminal-notifier -title "CPU use" -message "$(head -n5 <<< "$averages" | paste -sd / -)"
fi

If the average CPU use in the last three samples is over 50%, it uses terminal-notifier to display the processes that used the most CPU.

You could also use MenuMeters to display total CPU use in the menu bar:

Or run a script like this with GeekTool:

for i in {1..4}; do ps -erco %cpu,command | tail -n+2; sleep 1; done | sed 's/^ *//' | grep -v GeekTool | awk '{cpu=$1;sub(/[^ ]+ /,"");a[$0]+=cpu;c[$0]++}END{for(i in a){printf "%.1f %s\n",a[i]/c[$0],i}}' | sort -rn