Category filter
Script to manage running processes on Mac
The Activity Monitor on macOS devices provides users with easy access to view and monitor all processes running on the device. Running processes include apps, system apps used by macOS, and background processes. The Activity Monitor lists all these processes in real time, displaying their CPU usage, memory usage, energy usage, disk usage, and network usage. This document describes how admins can manage running processes on macOS devices using scripts. Utilize Hexnode UEM’s Execute Custom Script action to deploy the scripts.
Scripting Language – Bash
File extension – .sh
Show CPU utilization
1 |
top -l 1 -s 0 | grep "CPU usage" |
The above script retrieves the CPU utilization by the user processes, the system processes and the idle CPU percentage.
The top
command is a powerful tool for monitoring and troubleshooting system performance issues on a Mac. Executing the top
command from the device Terminal displays a dynamic, scrolling list of all the processes currently running on the Mac.
Show memory status
1 |
top -l 1 -s 0 | grep PhysMem |
Executing the above command will display the memory utilization by user processes at the time. The wired memory is also accounted for under the used memory.
List out currently running processes
1 |
ps aux |
The ps command displays the information about processes running on the device. Here is a brief description of what each column indicates.
- USER – Indicates if the process is a system or user process.
- PID – Process ID of the process.
- %CPU – Percentage of CPU utilized by the process.
- %MEM – Percentage of memory utilized by the process.
- VSZ – The Virtual Memory Size is the virtual memory size of the process in Kilo Bytes.
- RSS – The Resident Set Size shows how much non-swapped physical memory that a task has used.
- TT – Shows the terminal that executed the command associated with the process.
- STAT – Indicates the current status of the process.
- STARTED – Date on which the process started.
- TIME – Time at which the process initiated.
- COMMAND – The name of the command that was used to start the process.
1 |
ps aux | head -<count> |
Replace <count>
with the required number of processes to be displayed.
For example, ps aux | head -10
The above example command fetches a list of all the currently running processes but displays only the first 10 entries from the list.
Kill a process
1 |
kill -9 <PID> |
Kill a process by executing the above script after replacing <PID>
with the process ID (PID) of the required process. The process ID varies according to each session. Note that a process is allocated a new PID when it restarts.