Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
92 lines (62 sloc) 3.1 KB
comments_issue title
Getting Notified when a process is using 100% CPU on Mac OS X

{% include figure.html src="hotmacbook.jpg" %}{}

Lately I've noticed node.js occasionally spawning a process that's using 100% CPU. When this happens, the battery on my MacBook quickly drains.

17/08/2015 19:32:36.000 kernel[0]: process node[9119] caught causing excessive wakeups. Observed wakeups rate (per sec): 718; Maximum permitted wakeups rate (per sec): 150; Observation period: 300 seconds; Task lifetime number of wakeups: 45006

Now, I've not worked out what's starting this node.js process, but I have my suspicions that it's probably down to one of the plugins I'm using with Sublime.

So until I can work out what the cause is, let's treat the symptom: node.js uses 100% CPU and shags my battery.

My solution is creating a daemon that shows a notification when a process is using 100% CPU, so I can then find it and kill it.

Install terminal-notifier (github)

$ brew install terminal-notifier
$ brew linkapps terminal-notifier

Create a script to check if the CPU is being excessively used by a process

I found a nice simple script on Stack Overflow, which does exactly what I want.


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 / -)"

So save this to somewhere, I saved mine in ~/bin/

Now you need to make this script run periodically. This is done using launchdaemon.

Save the following plist somewhere, I saved mine in ~/Library/LaunchDaemons/com.omgmog.check100proc.plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

This plist will daemonise the that you created earlier, to make it run every 5 minutes (or 300 seconds).

You'll want to change the path to reflect wherever you saved the

Then load the plist with launchctl:

$ launchctl load -w ~/Library/LaunchDaemons/com.omgmog.check100proc.plist

That's it. You can check that the plist has been daemonised and is running using the following command:

$ launchctl list | grep check100proc