I'd like to automate that whenever a process is using more than 50% CPU
it sends a notification to my Notification Center
I'm using terminal-notifier for sending trough notifications
but I'm a bit stuck on what the best method is for creating this automation.
Should I use Automator.app or create a custom AppleScript and if so,
how do I make it to always be on?
If this is for interactive use, let me suggest a pragmatic alternative:
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:
bash
script that uses top
to find the highest-CPU-percentage task and invokes terminal-notifier
, if above the threshold.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:
As for scheduling the script to have launchd
run it on login: see https://stackoverflow.com/a/22872222/45375
The general format of launchd
*.plist
files is described at https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html or man launchd.plist
; StartInterval
is the key for specifying invocations every N seconds.
Step-by-step instructions for implementing the automated solution:
~/watchcpu
(i.e., file watchcpu
in your home folder), paste the above bash script into it, and save it.~/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>
launchctl load ~/Library/LaunchAgents/WatchCPU.plist
Note:
.plist
file MUST reside in ~/Library/LaunchAgents
in order to be loaded automatically at login.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).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With