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

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Show CPU utilization


Details of currently running 10 processes on Mac

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


Details of currently running 10 processes on Mac

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


Details of currently running processes on Mac

The ps command displays the information about processes running on the device. Here is a brief description of what each column indicates.

  1. USER – Indicates if the process is a system or user process.
  2. PID – Process ID of the process.
  3. %CPU – Percentage of CPU utilized by the process.
  4. %MEM – Percentage of memory utilized by the process.
  5. VSZ – The Virtual Memory Size is the virtual memory size of the process in Kilo Bytes.
  6. RSS – The Resident Set Size shows how much non-swapped physical memory that a task has used.
  7. TT – Shows the terminal that executed the command associated with the process.
  8. STAT – Indicates the current status of the process.
  9. STARTED – Date on which the process started.
  10. TIME – Time at which the process initiated.
  11. COMMAND – The name of the command that was used to start the process.

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.


Details of currently running 10 processes on Mac

Kill a process

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.

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository