Get Last Reboot Time

This script extracts the system boot time from the kernel and formats it in a human-readable format. It uses the sysctl command to query the kern.boottime value, which contains the timestamp of the last system boot. The output is formatted for Intune custom attributes to provide visibility into device uptime and reboot patterns.

Monitoring
Author: Ugur Koc
Version: 1.0

Quality checks

All checks pass
Last run May 11, 2026
  • ShellCheckPass

Tests run automatically on every change. What does each check mean?

View on GitHub
last-reboot.sh
#!/bin/bash

# TITLE: Get Last Reboot Time
# SYNOPSIS: Retrieves the last system reboot time on macOS
# DESCRIPTION: This script extracts the system boot time from the kernel and formats it
#              in a human-readable format. It uses the sysctl command to query the
#              kern.boottime value, which contains the timestamp of the last system boot.
#              The output is formatted for Intune custom attributes to provide visibility
#              into device uptime and reboot patterns.
# TAGS: Monitoring,Device
# PLATFORM: macOS
# MIN_OS_VERSION: 10.15
# AUTHOR: Ugur Koc
# VERSION: 1.0
# LASTUPDATE: 2025-06-04
# CHANGELOG:
#   1.0 - Initial release
#
# EXAMPLE:
#   ./last-reboot.sh
#   Outputs the last reboot time in YYYY-MM-DD HH:MM:SS format
#
# NOTES:
#   - Uses sysctl to query kern.boottime
#   - No external dependencies required
#   - Designed for Intune custom attributes (single line output)
#   - Time is displayed in local system timezone
#   - For more scripts and guides, visit: IntuneMacAdmins.com

# ============================================================================
# VARIABLES AND INITIALIZATION
# ============================================================================

# ============================================================================
# FUNCTIONS
# ============================================================================

# Function to output result (for Intune custom attributes)
output_result() {
    # For Intune custom attributes, output should be a single line
    echo "$1"
    exit 0
}

# Function to validate sysctl availability
check_prerequisites() {
    if ! command -v sysctl >/dev/null 2>&1; then
        output_result "Error: sysctl command not found"
    fi
}

# Function to get boot time
get_boot_time() {
    # Get the boot time from sysctl
    local boot_info
    boot_info=$(sysctl -n kern.boottime 2>/dev/null)

    if [[ -z "$boot_info" ]]; then
        output_result "Error: Unable to retrieve boot time"
    fi

    # Extract the timestamp (sec value)
    local timestamp
    timestamp=$(echo "$boot_info" | awk '{print $4}' | tr -d ',')

    # Validate timestamp
    if [[ ! "$timestamp" =~ ^[0-9]+$ ]]; then
        output_result "Error: Invalid boot time format"
    fi

    echo "$timestamp"
}

# ============================================================================
# MAIN SCRIPT LOGIC
# ============================================================================

main() {
    # Check prerequisites
    check_prerequisites

    # Get the boot timestamp
    local timestamp
    timestamp=$(get_boot_time)

    # Convert timestamp to formatted date
    local formatted_date
    formatted_date=$(date -r "$timestamp" "+%Y-%m-%d %H:%M:%S" 2>/dev/null)

    if [[ -z "$formatted_date" ]]; then
        output_result "Error: Unable to format boot time"
    fi

    # Calculate uptime for additional context
    local current_time
    current_time=$(date +%s)
    local uptime_seconds=$((current_time - timestamp))
    local uptime_days=$((uptime_seconds / 86400))
    local uptime_hours=$(((uptime_seconds % 86400) / 3600))

    # Format output with uptime information
    if [[ $uptime_days -gt 0 ]]; then
        output_result "Last Reboot: $formatted_date (${uptime_days}d ${uptime_hours}h ago)"
    else
        output_result "Last Reboot: $formatted_date (${uptime_hours}h ago)"
    fi
}

# ============================================================================
# ERROR HANDLING
# ============================================================================

# For Intune custom attributes - handle errors gracefully
trap 'output_result "Error: Script failed"' ERR

# ============================================================================
# SCRIPT EXECUTION
# ============================================================================

# Only run main if script is executed directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi

# Exit successfully (if not using output_result)
exit 0

Related Scripts

Discover similar scripts that might be useful for your automation needs

Highly Related

Apple Token Validity Checker

This script connects to Microsoft Graph and retrieves all Apple Device Enrollment Program (DEP) tokens and Apple Push Notification Certificates configured in Intune. It checks their validity status, expiration dates, and sync status to help administrators proactively manage Apple Business Manager integrations. The script generates detailed reports in CSV format, highlighting tokens and certificates that are expired, expiring soon, or have sync issues.

Monitoring
Highly Related

Check AppleCare Warranty Status

This script checks the warranty status of Apple devices by reading the local warranty information stored by macOS. It retrieves the coverage end date and displays it in a user-friendly format. The script is designed to work with Intune-managed macOS devices as a custom attribute.

Monitoring
Highly Related

Check Available Microsoft Updates

This script uses Microsoft AutoUpdate (MAU) to check for available updates for Microsoft Office applications and other Microsoft software on macOS. It runs the msupdate command in the context of the logged-in user to ensure proper access to user-specific update information. Results are formatted for Intune custom attributes to provide visibility into pending updates.

Monitoring