Tutorial

New updates and improvements to Roundfleet.

Important notice

The code examples and scripts provided in these tutorials are for educational purposes only. Roundfleet is not responsible for any issues, damages, or security vulnerabilities that may arise from the use, modification, or implementation of these examples. Always review and test code in a secure environment before using it in production systems.

Manage Running Processes on macOS

Monitor and control running processes on your Roundfleet 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 usage
  • ps - Snapshot of current processes
  • kill - 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

# Roundfleet Process Management Tool
# Monitor and manage processes across fleet devices

# Configuration
LOG_FILE="/var/log/roundfleet_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 "=== Roundfleet 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:

ColumnDescription
USERUser running the process (system or user process)
PIDProcess ID (unique identifier)
%CPUCPU usage percentage
%MEMMemory usage percentage
VSZVirtual memory size in KB
RSSPhysical memory usage in KB
TTTerminal associated with process
STATProcess status (R=running, S=sleeping, Z=zombie)
STARTEDProcess start date/time
TIMECPU time consumed
COMMANDCommand 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 "Roundfleet 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

Related Tutorials

Sleep and Power Management for macOS

Manage macOS sleep and power settings across Roundfleet devices with automated power policies, energy optimization, compliance monitoring, and comprehensive fleet-wide power management for enterprise efficiency and security.

Jul 7, 2025

Shutdown and Reboot Management for macOS

Manage macOS shutdown and reboot operations across Roundfleet devices with automated maintenance scheduling, graceful shutdown procedures, security validation, and comprehensive fleet-wide power management for enterprise operations.

Jul 7, 2025

Run Setup Assistant on macOS Devices at Next Reboot

Learn how to trigger macOS Setup Assistant on the next reboot for device reconfiguration, user onboarding, and enterprise deployment scenarios.

Jul 7, 2025

Scheduled Restart Management on macOS

Comprehensive guide to scheduling, managing, and monitoring system restarts across macOS fleets with advanced scheduling policies, user notifications, maintenance windows, and compliance reporting.

Jul 7, 2025

Battery Management on macOS

Learn how to monitor battery status, health, and performance on macOS devices using advanced battery management tools with enterprise-grade monitoring, optimization, compliance, and fleet-wide battery analytics capabilities.

Jul 7, 2025

Battery Charging Limit Management on macOS

Learn how to manage and configure battery charging limits on macOS devices using bclm (Battery Charge Limit Max) for enterprise battery health optimization and power management policies.

Jul 7, 2025

System Information Management on macOS

Collect, analyze, and manage system information across macOS devices using advanced system profiling, automated data collection, and enterprise analytics for comprehensive device inventory and monitoring.

Jul 7, 2025

Storage Management on macOS

Monitor disk usage, implement storage policies, and optimize storage performance across macOS devices using advanced storage management, automated cleanup, and enterprise analytics for organizational storage infrastructure.

Jul 7, 2025