Manage Running Processes on macOS
Monitor and control running processes on your MacFleet devices using command-line tools. This tutorial covers CPU monitoring, memory analysis, process listing, and process termination for effective system management.
Understanding macOS Process Management
macOS provides several command-line tools for process management:
top
- Real-time display of running processes and system usageps
- Snapshot of current processeskill
- Terminate processes by PID- Activity Monitor - GUI equivalent for visual monitoring
Show CPU Utilization
Basic CPU Usage
#!/bin/bash
# Display current CPU utilization
top -l 1 -s 0 | grep "CPU usage"
echo "CPU utilization retrieved successfully"
Detailed CPU Analysis
#!/bin/bash
# Get comprehensive CPU information
echo "=== CPU Utilization Analysis ==="
echo "Current CPU Usage:"
top -l 1 -s 0 | grep "CPU usage"
echo -e "\nTop CPU-consuming processes:"
top -l 1 -s 0 | head -20 | tail -10
echo -e "\nLoad average:"
uptime
Show Memory Status
Basic Memory Information
#!/bin/bash
# Display memory utilization
top -l 1 -s 0 | grep PhysMem
echo "Memory status retrieved successfully"
Comprehensive Memory Analysis
#!/bin/bash
# Detailed memory report
echo "=== Memory Status Analysis ==="
echo "Physical Memory Usage:"
top -l 1 -s 0 | grep PhysMem
echo -e "\nVirtual Memory Statistics:"
vm_stat
echo -e "\nMemory pressure:"
memory_pressure
List Running Processes
View All Processes
#!/bin/bash
# List all running processes
ps aux
echo "Process list generated successfully"
Limited Process List
#!/bin/bash
# Show top 10 processes
echo "Top 10 running processes:"
ps aux | head -11
# Show specific number of processes (replace 15 with desired count)
echo -e "\nTop 15 processes:"
ps aux | head -16
Filtered Process Views
#!/bin/bash
# Show only user processes
echo "User processes:"
ps aux | grep -v "^root"
echo -e "\nSystem processes:"
ps aux | grep "^root" | head -10
echo -e "\nProcesses using most CPU:"
ps aux --sort=-%cpu | head -10
echo -e "\nProcesses using most memory:"
ps aux --sort=-%mem | head -10
Process Termination
Kill Process by PID
#!/bin/bash
# Kill specific process (replace PID with actual process ID)
PID="1234"
if kill -9 "$PID" 2>/dev/null; then
echo "Process $PID terminated successfully"
else
echo "Failed to terminate process $PID or process not found"
fi
Kill Process by Name
#!/bin/bash
# Kill process by application name
APP_NAME="Safari"
if pkill -f "$APP_NAME"; then
echo "$APP_NAME processes terminated successfully"
else
echo "No $APP_NAME processes found or termination failed"
fi
Safe Process Termination
#!/bin/bash
# Gracefully terminate process with confirmation
terminate_process() {
local pid="$1"
local process_name
# Get process name
process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
if [[ -z "$process_name" ]]; then
echo "Process $pid not found"
return 1
fi
echo "Found process: $process_name (PID: $pid)"
# Try graceful termination first
if kill -TERM "$pid" 2>/dev/null; then
echo "Sent termination signal to $process_name"
sleep 3
# Check if process still exists
if kill -0 "$pid" 2>/dev/null; then
echo "Process still running, forcing termination..."
kill -9 "$pid" 2>/dev/null
fi
echo "Process $process_name terminated"
return 0
else
echo "Failed to terminate process $process_name"
return 1
fi
}
# Usage example (replace with actual PID)
# terminate_process 1234
Enterprise Process Management Script
#!/bin/bash
# MacFleet Process Management Tool
# Monitor and manage processes across fleet devices
# Configuration
LOG_FILE="/var/log/macfleet_processes.log"
ALERT_CPU_THRESHOLD=80
ALERT_MEMORY_THRESHOLD=80
# Logging function
log_action() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Get system statistics
get_system_stats() {
echo "=== System Statistics ==="
# CPU usage
local cpu_info
cpu_info=$(top -l 1 -s 0 | grep "CPU usage")
echo "CPU: $cpu_info"
# Memory usage
local mem_info
mem_info=$(top -l 1 -s 0 | grep PhysMem)
echo "Memory: $mem_info"
# Load average
echo "Load: $(uptime | awk -F'load average:' '{print $2}')"
# Top processes
echo -e "\nTop 5 CPU consumers:"
ps aux --sort=-%cpu | head -6 | tail -5
echo -e "\nTop 5 Memory consumers:"
ps aux --sort=-%mem | head -6 | tail -5
}
# Check for problematic processes
check_system_health() {
echo "=== System Health Check ==="
# Check high CPU usage processes
local high_cpu_procs
high_cpu_procs=$(ps aux | awk -v threshold="$ALERT_CPU_THRESHOLD" '$3 > threshold {print $2, $11, $3"%"}')
if [[ -n "$high_cpu_procs" ]]; then
echo "⚠️ High CPU usage processes:"
echo "$high_cpu_procs"
log_action "High CPU alert: $high_cpu_procs"
fi
# Check high memory usage processes
local high_mem_procs
high_mem_procs=$(ps aux | awk -v threshold="$ALERT_MEMORY_THRESHOLD" '$4 > threshold {print $2, $11, $4"%"}')
if [[ -n "$high_mem_procs" ]]; then
echo "⚠️ High memory usage processes:"
echo "$high_mem_procs"
log_action "High memory alert: $high_mem_procs"
fi
}
# Kill runaway processes
kill_runaway_processes() {
echo "=== Runaway Process Detection ==="
# Find processes using excessive CPU (>90% for >5 minutes)
local runaway_pids
runaway_pids=$(ps aux | awk '$3 > 90 {print $2}')
for pid in $runaway_pids; do
local process_name
process_name=$(ps -p "$pid" -o comm= 2>/dev/null)
if [[ -n "$process_name" ]]; then
echo "Found runaway process: $process_name (PID: $pid)"
# Skip critical system processes
if [[ "$process_name" =~ ^(kernel|launchd|WindowServer)$ ]]; then
echo "Skipping critical system process: $process_name"
continue
fi
log_action "Terminating runaway process: $process_name (PID: $pid)"
kill -TERM "$pid"
fi
done
}
# Main monitoring function
main() {
log_action "=== MacFleet Process Monitoring Started ==="
get_system_stats
echo ""
check_system_health
echo ""
# Optionally kill runaway processes (uncomment to enable)
# kill_runaway_processes
log_action "=== Process monitoring completed ==="
}
# Execute main function
main "$@"
Process Information Details
When using ps aux
, each column provides specific information:
Column | Description |
---|---|
USER | User running the process (system or user process) |
PID | Process ID (unique identifier) |
%CPU | CPU usage percentage |
%MEM | Memory usage percentage |
VSZ | Virtual memory size in KB |
RSS | Physical memory usage in KB |
TT | Terminal associated with process |
STAT | Process status (R=running, S=sleeping, Z=zombie) |
STARTED | Process start date/time |
TIME | CPU time consumed |
COMMAND | Command used to start the process |
Common Process Management Tasks
Find Process by Name
# Find Safari processes
ps aux | grep -i safari
# Find all processes containing "mail"
ps aux | grep -i mail | grep -v grep
Monitor Process in Real-time
# Watch specific process
watch -n 1 'ps aux | grep -i safari'
# Monitor system resources continuously
top -s 1 -n 10
Generate Process Report
#!/bin/bash
# Create comprehensive process report
REPORT_FILE="process_report_$(date +%Y%m%d_%H%M%S).txt"
{
echo "MacFleet Process Report"
echo "Generated: $(date)"
echo "Hostname: $(hostname)"
echo "=========================="
echo ""
echo "System Overview:"
top -l 1 -s 0 | grep -E "(CPU usage|PhysMem)"
echo ""
echo "Top 20 Processes:"
ps aux | head -21
echo ""
echo "Process Count by User:"
ps aux | awk '{print $1}' | sort | uniq -c | sort -nr
} > "$REPORT_FILE"
echo "Process report saved to: $REPORT_FILE"
Important Notes
- Process IDs (PIDs) change when processes restart
- System processes require admin privileges to terminate
- Force kill (
kill -9
) should be used as last resort - Monitor resource usage before terminating processes
- Test scripts on individual devices before fleet deployment