Back to Resources

Level Verified

macOS Monitor - System Time Check

Created by

Level

Type

Script

Category

General

Platforms
WindowsApple iOSLinux

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.

Description

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.

Script

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

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.

FAQ

  • 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.

Included with this Script:

Below is a list of what you can expect to find when importing this Script.

Script details:

The following data and settings will be imported with your script.

Script Name

macOS Monitor - System Time Check

Description

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.

Language

Bash

Timeout (In Seconds)

100

Run As

Local system

Import into Level

Related resources

Explore more automations, scripts, and policies to further enhance your IT operations.

View all resources