Skip to content

Instantly share code, notes, and snippets.

@quinncomendant
Last active September 18, 2022 14:40
Show Gist options
  • Save quinncomendant/803eeaa291c390202a74d208836e6bb8 to your computer and use it in GitHub Desktop.
Save quinncomendant/803eeaa291c390202a74d208836e6bb8 to your computer and use it in GitHub Desktop.
Detect if Microsoft Edge has been using more than 90% CPU continuously.
#!/usr/bin/env bash
# Microsoft Edge on macOS sometimes will get stuck using 100% CPU endlessly, requiring a relaunch to stop.
# See issue at https://techcommunity.microsoft.com/t5/discussions/edge-browser-100-cpu-requires-force-quit-macos-mojave-and-v91-0/m-p/2441462/highlight/true#M48074
#
# This script will display a macOS notification if Microsoft Edge has been using more
# than 90% CPU continuously for more than 10 minutes. It only checks the main
# Microsoft Edge process, not the Microsoft Edge Helper processes.
#
# Save it to ~/bin/cpu-threshold-alert-microsoft-edge
# Make it executable: chmod 755 ~/bin/cpu-threshold-alert-microsoft-edge
# Run it in a loop in the terminal:
#
# while true; do ~/bin/cpu-threshold-alert-microsoft-edge; sleep 5; done
#
# Or add it to `crontab -e`:
#
# * * * * * $HOME/bin/cpu-threshold-alert-microsoft-edge
#
cpu_threshold=90;
duration=600;
scriptname=$(basename "$0");
statefile="/tmp/cpu-threshold-alert-microsoft-edge";
cpu=$(/bin/ps -Ao pcpu,command | grep -- "Microsoft Edge.app/Contents/MacOS/Microsoft Edge" | grep -Ev "grep --|$scriptname" | awk '{printf "%.0f", $1}');
if [[ -z $cpu ]]; then
echo "Microsoft Edge not running" 1>&2;
exit 1;
fi
if [[ $cpu -gt $cpu_threshold ]]; then
touch "$statefile";
if [[ $(stat -f %SB -t %s "$statefile") -lt $(($(date +%s) - duration)) ]]; then
message="Edge using over ${cpu_threshold}% CPU for ${duration} seconds since $(stat -f %SB -t '%F %T' "$statefile"). Report this issue: go to “Help” menu → “Send Feeback…”.";
osascript -e "display notification \"$message\" with title \"$scriptname\" sound name \"Pop\"";
rm -f "$statefile";
fi
else
rm -f "$statefile";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment