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.
#!/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.