Managing Processes on macOS: Commands and Examples
Working with processes on macOS requires familiarity with the Terminal. This guide covers beginner to advanced commands for monitoring, listing, and managing processes efficiently. You'll learn to identify high memory and CPU usage processes, count running processes, and handle processes based on specific names or patterns.
A) Getting Started: Basic Commands for Process Management
1. List Running Processes in macOS
To see all currently running processes, use the ps -ax
command:
ps -ax
This command lists all running processes, showing their Process ID (PID) and command names.
2. Detailed Process List Using ps aux
For a more detailed view, including memory and CPU usage, try:
ps aux
This displays the user who started each process, along with memory and CPU usage.
3. Continuously Monitor Processes with top
Command
The top
command provides a live, updating view of active processes:
top
This is useful for identifying which processes are using the most resources in real-time. Press q
to exit.
Options for Customizing top
Output
-a: Show Accumulated Usage
Displays accumulated CPU usage statistics rather than a snapshot at each refresh:
top -a
-d: Display Diagnostics for Processes
Enables diagnostic information about processes, which can be useful for troubleshooting:
top -d
-e: Enhanced Mode
This option provides extended information about processes:
top -e
-c: Customizing Output Columns
Controls which columns appear by setting <mode>
values to customize views (e.g., basic
, normal
):
top -c normal
-F and -f: Toggle Display of Forked Processes
The -F
option includes all forked processes, while -f
excludes them:
top -F
top -f
-h: Display Help
Shows all available options for top
:
top -h
Advanced Configuration
-i: Set Refresh Interval
Defines the refresh rate in seconds. For example, setting interval
to 5 will refresh every 5 seconds:
top -i 5
-l: Limit Number of Refresh Samples
Use -l
to specify the number of samples before top
exits. For instance, setting it to 3 captures three refresh cycles and then exits:
top -l 3
-ncols: Limit Displayed Columns
Limits the number of columns in the output. This is useful for fitting top
output on smaller screens:
top -ncols 80
Sorting and Filtering Options Using top
-o: Sort by Primary Key
Sorts by a specific field, such as cpu
or mem
. Here’s an example sorting by CPU usage:
top -o cpu
-O: Sort by Secondary Key
Sets a secondary sort key, useful if two fields are of interest. For example, sort by CPU first, then memory:
top -o cpu -O mem
-R and -r: Reverse Sort Order
Use -R
or -r
to reverse the sort order. For example, display CPU in descending order:
top -o cpu -R
Controlling Refresh and Sample Rates Using top
-S: Cumulative Mode
Displays accumulated CPU time per process:
top -S
-s: Adjust Delay Between Refreshes
Sets the delay time between refresh cycles, useful for slower or faster monitoring. For example, a delay of 3 seconds:
top -s 3
-n: Limit Number of Displayed Processes
Limits the output to show only the top n
processes by usage:
top -n 5
Statistical Output Options Using top
-stats: Custom Stats Display
Customize which statistics are shown by specifying key(s)
. For example, display only CPU and memory usage:
top -stats cpu,mem
-pid: Filter by Process ID
Show details for a specific process ID only:
top -pid 1234
-user: Filter by Username
Displays processes owned by a specific user:
top -user john
-U: List Processes by Username
Similar to -user
, but displays only those processes running under the specified user:
top -U john
-u: Display Only User Processes
Shows only user processes, filtering out system processes:
top -u
B) Intermediate: Analyzing High Resource Consumption Processes
1. Listing High Memory Usage Processes
To list processes with high memory usage, combine ps
with sorting:
ps aux --sort=-%mem | head
This command sorts processes by memory usage in descending order and displays the top results. Adjust head
count to show more or fewer processes.
2. Listing High CPU Usage Processes
To list processes that consume the most CPU, use:
ps aux --sort=-%cpu | head
Like the previous command, this sorts by CPU usage, helping identify processes that could be slowing down your Mac.
C) Advanced: Regex Filtering, Killing Processes, and System Statistics
1. Listing and Killing Processes by Pattern (Regex)
To list and kill processes by matching names or patterns, combine ps
with grep
. For example, to target a process containing "chrome" in its name:
ps aux | grep chrome
This shows any processes with "chrome" in the command name. To kill all matched processes, use:
pkill -f chrome
The -f
flag ensures that pkill
looks at the full command string for matches, terminating each matched process.
2. Counting Total Running Processes
For a total count of running processes, use:
ps -ax | wc -l
This command counts the lines outputted by ps -ax
, giving you the total number of processes.
3. Calculating Total Memory and CPU Usage by Pattern
To calculate total memory and CPU usage of processes matching a specific pattern, use awk
and grep
:
ps aux | grep chrome | awk '{sum_mem += $4; sum_cpu += $3} END {print "Total Memory:", sum_mem, "%"; print "Total CPU:", sum_cpu, "%"}'
This command filters processes by "chrome," then calculates the total memory and CPU usage as percentages.
Process Management on macOS: Diagram of Components
This diagram outlines the major components involved in process management on macOS. The Kernel serves as the core, interfacing between hardware and software. The Memory Manager handles memory allocation, while the Scheduler prioritizes and assigns CPU resources to processes based on system demand.
Dont SPAM