Level Verified

macOS Monitor - System Time Check

Ensures macOS endpoints maintain the correct time zone, network time sync, and NTP server configuration. Fixes issues automatically and alerts you when inconsistencies occur, helping prevent authentication, logging, and scheduling conflicts.

Import into Level

Problem overview

Incorrect or inconsistent system time on macOS endpoints can cause authentication failures, inaccurate logging, and missed scheduled tasks. By automatically checking and aligning the system time settings, this script prevents disruptions that stem from time zone or network time configuration errors.

Bash 100s timeout Runs as Local system macOS
#!/bin/bash

# This resource is provided as a convenience for Level users. We cannot 
# guarantee it will work in all environments. Please test before deploying 
# to your production environment. We welcome contributions to our community 
# library

# Level Library
# https://level.io/library/script-macos-monitor-system-time-check

# Set the target time zone
TARGET_TIMEZONE="{{cf_timezone}}"

# Variables to hold checks
check_count=0
checks_failed=""

# Check and display the current time zone
timezone=$(sudo systemsetup -gettimezone | awk '{print $3}')
echo "Current Time Zone: $timezone"

# Check if the time zone matches the target
if [[ "$timezone" != "$TARGET_TIMEZONE" ]]; then
  echo "ALERT: Time zone is not $TARGET_TIMEZONE. Attempting to set to $TARGET_TIMEZONE..."
  sudo systemsetup -settimezone "$TARGET_TIMEZONE"
  if [[ $? -eq 0 ]]; then
    echo "Time zone set to $TARGET_TIMEZONE successfully."
  else
    echo "Failed to set time zone to $TARGET_TIMEZONE."
    check_count=$((check_count + 1))
    checks_failed+=" Failed to set time zone to $TARGET_TIMEZONE."
  fi
fi

# Check if the system clock is synchronized
ntp_status=$(sudo systemsetup -getusingnetworktime | awk '{print $3}')
if [[ "$ntp_status" != "On" ]]; then
  echo "ALERT: Network time synchronization is not enabled."
  check_count=$((check_count + 1))
  checks_failed+=" Network time synchronization is not enabled."
fi

# Check if the NTP server is configured
ntp_server=$(sudo systemsetup -getnetworktimeserver | awk '{print $3}')
if [[ -z "$ntp_server" ]]; then
  echo "ALERT: No NTP server is configured."
  check_count=$((check_count + 1))
  checks_failed+=" No NTP server is configured."
fi

# Final check summary
if [ $check_count -gt 0 ]; then
  echo "ALERT: $check_count checks failed."
  echo "Checks Failed: $checks_failed"
  exit 1
else
  echo "SUCCESS: All checks passed."
  exit 0
fi

The script verifies whether the current time zone matches your preferred setting, attempting to correct it if necessary. It then checks if network time sync is enabled, confirms that an NTP server is set, and flags any issues encountered. Running with root-level permissions, it ensures these configurations are updated effectively to preserve accurate and consistent system time.

Use cases

  • Standardizing the time zone on all macOS endpoints
  • Preventing drift that leads to logging and audit trail discrepancies
  • Automating remedial actions for unconfigured or misconfigured NTP servers
  • On-demand or scheduled checks to ensure reliable system time for sensitive applications

Recommendations

  • Test Before Production: Run this script on a test device first to confirm correct time settings.
  • Target Time Zone Configuration: Adjust TARGET_TIMEZONE in the script to your preferred time zone.
  • On-Demand Trigger: Use a script-based monitor in Level to automatically run this script when certain conditions are met.
  • Scheduled Maintenance: Build a scheduled automation within Level to routinely confirm consistent time settings across all macOS endpoints.
  • Connectivity Checks: Verify that your network allows traffic to the configured NTP server for smooth synchronization.

Frequently asked questions.

What if the script cannot set the specified time zone?

It will log a failure and exit with an error, allowing you to investigate permission or system setting issues.

Is network time sync always enforced?

The script checks if it’s enabled. If disabled, it raises an alert so you can manually enable it or automate the fix in your environment.

Do I need specific permissions for this script?

All scripts in Level run with root-level permissions, ensuring they can modify system settings when necessary.

How frequently should I run it?

It depends on your environment’s needs. If frequent time conflicts occur, a daily or weekly schedule can proactively catch misconfiguration.

Can I modify the NTP server within this script?

Absolutely. Just replace the default configuration with your preferred server in the macOS settings or directly in the script if needed.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.