Skip to content

Instantly share code, notes, and snippets.

@andreadellacorte
Created May 27, 2025 10:14
Show Gist options
  • Save andreadellacorte/235c1cf8cb6c2d2271783d9959515e0d to your computer and use it in GitHub Desktop.
Save andreadellacorte/235c1cf8cb6c2d2271783d9959515e0d to your computer and use it in GitHub Desktop.
mac diagnostic script; saves a log file to desktop
#!/bin/bash
# Resolve the actual user running the script, even under sudo
USER_HOME=$(eval echo ~$(logname))
LOGFILE="$USER_HOME/Desktop/mac_diagnostics_$(date +%Y%m%d_%H%M%S).log"
echo "📋 Writing diagnostics to: $LOGFILE"
# Begin log file
echo "== Mac Diagnostics Report - $(date) ==" > "$LOGFILE"
# Append section
log() { echo -e "\n== $1 ==" | tee -a "$LOGFILE"; }
log "Model Info"
system_profiler SPHardwareDataType | tee -a "$LOGFILE"
log "CPU Info"
sysctl -n machdep.cpu.brand_string | tee -a "$LOGFILE"
top -l 1 -n 5 -s 1 -stats pid,command,cpu | tee -a "$LOGFILE"
log "Memory (vm_stat)"
vm_stat | tee -a "$LOGFILE"
echo -e "\n== Top RAM Usage ==" | tee -a "$LOGFILE"
ps -A -o %mem,rss,comm | sort -nr | head | tee -a "$LOGFILE"
log "Disk Usage"
df -h | tee -a "$LOGFILE"
log "Disk Info"
diskutil info / | tee -a "$LOGFILE"
log "Spotlight Indexing"
mdutil -sa | tee -a "$LOGFILE"
log "Thermal Throttling (pmset – snapshot with timeout)"
if command -v gtimeout >/dev/null; then
(gtimeout 2 pmset -g thermlog || echo "⚠️ pmset hang avoided") | tail -20 | tee -a "$LOGFILE"
else
echo "⚠️ gtimeout not found. Skipping safe thermal log read." | tee -a "$LOGFILE"
fi
log "powermetrics (safe sample, 10s)"
if command -v powermetrics >/dev/null; then
sudo powermetrics --samplers smc --sample-count 2 --sample-rate 5000 | tee -a "$LOGFILE"
else
echo "⚠️ powermetrics not found." | tee -a "$LOGFILE"
fi
log "Battery Info"
pmset -g batt | tee -a "$LOGFILE"
system_profiler SPPowerDataType | grep -i "Cycle Count\|Condition" | tee -a "$LOGFILE"
log "Launch Agents and Daemons"
ls ~/Library/LaunchAgents 2>/dev/null | tee -a "$LOGFILE"
ls /Library/LaunchAgents 2>/dev/null | tee -a "$LOGFILE"
ls /Library/LaunchDaemons 2>/dev/null | tee -a "$LOGFILE"
log "Running User Services"
launchctl list | grep -v "\-" | tee -a "$LOGFILE"
log "System Errors (Last 1 Hour)"
log show --predicate 'eventMessage contains "error"' --last 1h 2>/dev/null | tee -a "$LOGFILE"
echo -e "\n✅ Done. Diagnostics saved to: $LOGFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment