top - macOS


Overview

top provides a real-time view of system processes, their resource consumption, and performance metrics. It’s an essential tool for monitoring and troubleshooting system issues.

Syntax

top [options]

Options/Flags

  • -C: Only show the command line that executed the process. Default is the full command line including arguments.
  • -F: Force an update of the screen and don’t allow any scrolling.
  • -P: Specify the PID(s) of the process(es) to monitor. Can be used multiple times for multiple PIDs.
  • -S: Sort by a specific field. Default is %CPU.
  • -d: Seconds between screen updates. Default is 3 seconds.
  • -l: Long format. Display additional information about each process.
  • -n: Number of processes to display. Default is all processes.
  • -o: Field to sort by. See top -S for available fields.
  • -s: Cumulative CPU time (in seconds) for each process.
  • -u: Specify the user(s) to display processes for. Can be used multiple times for multiple users.
  • -R: A less detailed (but faster) view.
  • -q: Quiet mode. Turn off most output, including headers.

Examples

  • Basic usage: Monitor all system processes:

    top
    
  • Filter by user: Show processes owned by a specific user:

    top -u username
    
  • Sort by memory usage:

    top -o rsize
    
  • Long format with cumulative CPU time:

    top -l -s
    

Common Issues

  • Permission denied: If you’re not seeing all processes, ensure you have sufficient permissions (e.g., sudo).
  • Slow performance: top can be resource-intensive, especially when monitoring a large number of processes. If you experience lag, try using -F or reduce the update frequency with -d.
  • Process names truncated: If process names are cut off, use -l for a longer view.

Integration

  • Piping output: Send the output of top to other commands for analysis, such as:
    top -l -s | sort -nrk 6
    
  • Monitoring scripts: Create scripts that periodically run top and parse the output to monitor system health.
  • Homebrew package: Install htop for an enhanced terminal-based process viewer with additional features.
  • ps: Lists all active processes.
  • kill: Terminates a process.
  • vmstat: Provides statistics about virtual memory usage.