# 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](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) 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
--------------------

Script to display the current CPU utilization of the device

top -l 1 -s 0 | grep "CPU usage" 

   1

  top -l 1 -s 0 | grep "CPU usage" 

   

 

  

![Details of currently running 10 processes on Mac ](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/03/CPU-Utilization.png)
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 
-------------------

Script to display the memory utilization of the device

top -l 1 -s 0 | grep PhysMem 

   1

  top -l 1 -s 0 | grep PhysMem 

   

 

  

[![View the memory utilization of user processes on Mac from the Hexnode UEM console](https:2024/10/Memory-utilization-by-user-processes-on-Mac.png "Memory utilization by user processes on Mac")](https:2024/10/Memory-utilization-by-user-processes-on-Mac.png)

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 
-------------------------------------

Script to list the currently running processes on the device

ps aux 

   1

  ps aux 

   

 

  

![Details of currently running processes on Mac](https:2023/03/List-running-processes.png)
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.

Script to list the currently running processes on the device

ps aux | head -<count> 

   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.

![Details of currently running 10 processes on Mac](https:2023/03/Currently-running-10-processes.png)
Kill a process 
---------------

Script to kill a process on the device

kill -9 <PID> 

   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.

 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.