Skip to content

cnshsliu/MacProcessHealthMonitor

Repository files navigation

Mac Process Health Monitor

A tiny native macOS utility written in SwiftUI that watches for processes with sustained, abnormally high CPU usage and lets you terminate them — manually or automatically.

The Scenario

If you spend time in Activity Monitor, you have probably seen a process stick at over 100% CPU for a long stretch. Most of the time that is not normal multi-core work; it is a hung or buggy process spinning in an internal loop. Keeping it alive burns battery, generates heat, and serves no purpose. This tool detects that exact pattern — CPU above a threshold you choose, for a duration you choose — and either lists the offender or kills it for you.

What It Does

  • Samples all processes every 3 seconds using the system top command.
  • Flags any process whose CPU usage stays above your threshold long enough.
  • Shows flagged processes in a live table with PID, name, owner, CPU %, and how long it has been high.
  • Provides an Auto-kill toggle:
    • Off: you review the list and click Force Quit yourself.
    • On: the app runs kill -9 automatically as soon as a process qualifies.
  • For processes owned by other users (e.g. root/system daemons), a normal kill is attempted first; if that fails, it escalates via osascript with administrator privileges.
  • Remembers your threshold, duration, and auto-kill choice in UserDefaults so they survive restarts.

Build & Run

1. Build the release binary

cd ~/dev/mac-process-health-monitor
swift build -c release

2. Bundle it as a real macOS app

A bare SPM executable does not get a Dock icon or window from the system. Wrap it as an .app:

cd ~/dev/mac-process-health-monitor
rm -rf MacProcessHealthMonitor.app
mkdir -p MacProcessHealthMonitor.app/Contents/MacOS
mkdir -p MacProcessHealthMonitor.app/Contents/Resources
cp .build/release/MacProcessHealthMonitor MacProcessHealthMonitor.app/Contents/MacOS/
codesign --force --deep --sign - MacProcessHealthMonitor.app

The Info.plist bundled in this repo is used automatically.

3. Launch

open MacProcessHealthMonitor.app

Or double-click MacProcessHealthMonitor.app in Finder.

First-Run Gatekeeper Note

Because the app is only ad-hoc signed and not notarized, macOS may refuse to open it the first time. If you see a "cannot be opened" dialog, run:

xattr -cr MacProcessHealthMonitor.app
open MacProcessHealthMonitor.app

Or go to System Settings → Privacy & Security and click Open Anyway.

Safety Notes

  • Auto-kill is off by default.
  • The app never targets itself or the kernel task (kernel_task, PID 0).
  • Killing root-owned processes triggers a system password prompt via osascript; the app never stores or handles passwords itself.
  • Use sensible thresholds. Some legitimate workloads briefly spike above 100% CPU on multi-core machines; that is why the tool cares about sustained usage, not single-sample bursts.

Project Structure

MacProcessHealthMonitor/
├── Package.swift
├── README.md
├── MacProcessHealthMonitor.app/          # bundled after build
│   └── Contents/
│       ├── Info.plist
│       └── MacOS/MacProcessHealthMonitor
└── Sources/MacProcessHealthMonitor/
    ├── MacProcessHealthMonitorApp.swift
    ├── ContentView.swift
    ├── ProcessMonitor.swift
    └── ProcessInfo.swift

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages