If this is for interactive use, let me suggest a pragmatic alternative:
- Run Activity Monitor.
- Control-click its dock icon.
- Select
Dock Icon > Show CPU Usage
- or, for a floating window, Monitors > Show CPU Usage
.
You'll get a per-core display of current CPU usage - clicking on it will show the full Activity Monitor window, where you can sort by CPU usage.
If you do need an automated solution, I suggest:
- writing a
bash
script that uses top
to find the highest-CPU-percentage task and invokes terminal-notifier
, if above the threshold.
- scheduling that script as a
launchd
task for periodic invocation.
Automator and AppleScript are probably too heavy for such - presumably frequent - background activity.
Even running top
itself uses quite a bit of CPU.
Here's a simple bash script that roughly does what you want:
#!/usr/bin/env bash
read pct name < <(top -l 2 -n 1 -F -o cpu -stats cpu,command | tail -1)
if (( ${pct%.*} >= 50 )); then
/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier \
-message "Process > 50%: $name ($pct%)"
fi
Note that this takes at least 2 seconds to run, because 2 samples (1 second apart) must be collected to calculate CPU-usage percentages, so consider that when determining how frequently to invoke the command.
Update - see below for step-by-step implementation instructions.
References:
Step-by-step instructions for implementing the automated solution:
- Create the bash script:
- Create plain-text file
~/watchcpu
(i.e., file watchcpu
in your home folder), paste the above bash script into it, and save it.
- Create the per-user launch agent for invocation at login and periodic invocation thereafter:
- Create plain-text file
~/Library/LaunchAgents/WatchCPU.plist
, paste the following XML document into it, and save it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>WatchCPU</string>
<key>ProgramArguments</key>
<array>
<string>bash</string>
<string>-c</string>
<string>. ~/watchcpu</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>15</integer>
</dict>
</plist>
- Load the per-user launch agent to activate it:
- Run the following command in Terminal (only needed once; from then on, the file will auto-load on every login):
launchctl load ~/Library/LaunchAgents/WatchCPU.plist
Note:
- You're free to choose your own filenames and a different location for your bash script, but the launch agent
.plist
file MUST reside in ~/Library/LaunchAgents
in order to be loaded automatically at login.
- The interval (key
StartInterval
) is chosen at 15 seconds; again, you're free to change that, but note that choosing more frequent invocations doesn't make much sense, because launchd
(the service that invokes launch agents) throttles agents whose execution time is too close to the invocation interval; I'm unclear on the details, but in the solution at hand an interval of 10 seconds results in frequent throttling notices in system.log
(check via Console.app).