Skip to content

Instantly share code, notes, and snippets.

@KMJ-007
Created March 22, 2026 15:06
Show Gist options
  • Select an option

  • Save KMJ-007/5e2118e7d9bc4650cb4f0262afcd68c3 to your computer and use it in GitHub Desktop.

Select an option

Save KMJ-007/5e2118e7d9bc4650cb4f0262afcd68c3 to your computer and use it in GitHub Desktop.
macOS notification when CPU usage is high

High CPU Alert for macOS

Get a notification when any process uses too much CPU for 30+ seconds.

Setup (1 minute)

1. Create the script:

mkdir -p ~/bin
cat > ~/bin/cpu_alert.sh << 'EOF'
#!/bin/bash
THRESHOLD=80
COUNT=0
while true; do
  HIGH=$(ps -eo %cpu,comm | awk -v t=$THRESHOLD '$1 > t && $2 != "bash" && $2 != "ps" && $2 != "cpu_alert" {print $2" is making your CPU go brrr at "$1"%"}')
  if [ -n "$HIGH" ]; then
    ((COUNT++))
    if [ $COUNT -eq 3 ]; then
      osascript -e 'display notification "'"$HIGH"'" with title "High CPU Alert"'
      COUNT=0
    fi
  else
    COUNT=0
  fi
  sleep 10
done
EOF
chmod +x ~/bin/cpu_alert.sh

2. Create launchd agent (runs after restart):

cat > ~/Library/LaunchAgents/com.karanjanthe.cpu_alert.plist << 'EOF'
<?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>Label</key>
    <string>com.karanjanthe.cpu_alert</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/Users/karanjanthe/bin/cpu_alert.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
EOF

3. Load it:

launchctl load ~/Library/LaunchAgents/com.karanjanthe.cpu_alert.plist

To stop it:

launchctl unload ~/Library/LaunchAgents/com.karanjanthe.cpu_alert.plist

To remove completely:

launchctl unload ~/Library/LaunchAgents/com.karanjanthe.cpu_alert.plist
rm ~/Library/LaunchAgents/com.karanjanthe.cpu_alert.plist
rm ~/bin/cpu_alert.sh

Customization

  • Change THRESHOLD=80 → different percentage
  • Change COUNT==3 → more/less checks before alert (3 checks = 30 seconds)
  • Change sleep 10 → faster/slower checking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment