Back to Resources
Level
Script
General
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.
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.
1#!/bin/bash
2
3# This resource is provided as a convenience for Level users. We cannot
4# guarantee it will work in all environments. Please test before deploying
5# to your production environment. We welcome contributions to our community
6# library
7
8# Level Library
9# https://level.io/library/script-macos-monitor-system-time-check
10
11# Set the target time zone
12TARGET_TIMEZONE="{{cf_timezone}}"
13
14# Variables to hold checks
15check_count=0
16checks_failed=""
17
18# Check and display the current time zone
19timezone=$(sudo systemsetup -gettimezone | awk '{print $3}')
20echo "Current Time Zone: $timezone"
21
22# Check if the time zone matches the target
23if [[ "$timezone" != "$TARGET_TIMEZONE" ]]; then
24 echo "ALERT: Time zone is not $TARGET_TIMEZONE. Attempting to set to $TARGET_TIMEZONE..."
25 sudo systemsetup -settimezone "$TARGET_TIMEZONE"
26 if [[ $? -eq 0 ]]; then
27 echo "Time zone set to $TARGET_TIMEZONE successfully."
28 else
29 echo "Failed to set time zone to $TARGET_TIMEZONE."
30 check_count=$((check_count + 1))
31 checks_failed+=" Failed to set time zone to $TARGET_TIMEZONE."
32 fi
33fi
34
35# Check if the system clock is synchronized
36ntp_status=$(sudo systemsetup -getusingnetworktime | awk '{print $3}')
37if [[ "$ntp_status" != "On" ]]; then
38 echo "ALERT: Network time synchronization is not enabled."
39 check_count=$((check_count + 1))
40 checks_failed+=" Network time synchronization is not enabled."
41fi
42
43# Check if the NTP server is configured
44ntp_server=$(sudo systemsetup -getnetworktimeserver | awk '{print $3}')
45if [[ -z "$ntp_server" ]]; then
46 echo "ALERT: No NTP server is configured."
47 check_count=$((check_count + 1))
48 checks_failed+=" No NTP server is configured."
49fi
50
51# Final check summary
52if [ $check_count -gt 0 ]; then
53 echo "ALERT: $check_count checks failed."
54 echo "Checks Failed: $checks_failed"
55 exit 1
56else
57 echo "SUCCESS: All checks passed."
58 exit 0
59fi
macOS Monitor - System Time Check
This script checks and ensures that the macOS system is using the correct time zone and has network time synchronization enabled. If the time zone does not match the target, it attempts to update it, and it also verifies whether an NTP server is configured. If any of these checks fail, it alerts the user and exits with an error code.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.