What This Script Does

The auto-update-brew.sh script automates the routine maintenance of your Homebrew packages. Keeping your packages up to date is important for security patches, bug fixes, and access to new features, but remembering to run updates manually can be tedious. This script handles the entire update cycle automatically and sends you a text message summary when it finishes, so you always know the current state of your system without having to check manually.

Before performing any updates, the script runs a series of pre-flight checks to ensure conditions are appropriate for an unattended update. The first check verifies your Wi-Fi connection. If you have configured a WIFI_NETWORK in your config file, the script compares your current network name against that setting. This prevents the script from downloading potentially large updates over a metered cellular hotspot or a slow public Wi-Fi connection. If the network does not match, the script exits gracefully and logs the reason.

The second pre-flight check examines your Mac's power status. Running package updates can be CPU-intensive and may take several minutes, which is not ideal when your Mac is running on battery power. If REQUIRE_AC_POWER is enabled in your config, the script verifies that your Mac is plugged into an AC adapter before proceeding. If you are on battery power, the update is skipped and rescheduled for the next scheduled run.

Once the pre-flight checks pass, the script performs three core Homebrew operations in sequence. First, it runs brew update, which downloads the latest package index from Homebrew's repositories. This tells Homebrew what new versions are available but does not install anything yet. Second, it runs brew upgrade, which actually installs newer versions of any outdated packages. Each package upgrade is logged individually so you can see exactly what changed. Third, it runs brew cleanup, which removes old versions of packages that are no longer needed and clears the download cache to free up disk space.

After all three operations complete, the script compiles a summary of the results. This summary includes the number of packages that were updated, any errors that occurred during the process, and the amount of disk space recovered by the cleanup step. The summary is then sent to your phone as an iMessage text message using macOS's built-in osascript command to interface with the Messages app. This gives you a quick at-a-glance notification that your system is up to date.

The script is designed to be run either manually or on a schedule via launchd. When run manually, you see all output in real time in your terminal. When run on a schedule, the output is captured in a log file and the text message notification serves as your primary status report. For scheduling, see the Schedule Auto-Updates script, which automates the creation of the launchd agent.

Usage

You can run this script manually at any time, or schedule it to run automatically using the setup-auto-update.sh script.

# Preview what would be updated (no changes made)
./auto-update-brew.sh --dry-run

# Run the update immediately
./auto-update-brew.sh

When running manually, you will see progress output in your terminal as each Homebrew operation completes. The text message notification is sent at the very end, after all operations have finished.

Options and Flags

Flag Description
--dry-run Simulate the update process. Shows which packages are outdated and what would be updated, but does not install or remove anything.

Configuration

The following settings in your config/homebrew-scripts.conf file affect this script's behavior:

Setting Effect
WIFI_NETWORK The name of your preferred Wi-Fi network. Updates only run when connected to this network. Leave empty to allow updates on any network.
PHONE_NUMBER Your iMessage-compatible phone number for receiving text message notifications. Include the country code (e.g., +1 for US numbers).
REQUIRE_AC_POWER When set to true, the script only runs when your Mac is connected to AC power. Prevents updates from draining battery.

If PHONE_NUMBER is not configured, the script still performs all update operations but skips the text message notification step. All results are still recorded in the log file regardless of the notification setting.

What Gets Changed

  • Homebrew packages — All installed formulae and casks are updated to their latest available versions via brew upgrade.
  • Old package versions — Previous versions of upgraded packages are removed by brew cleanup to reclaim disk space.
  • Download cache — Cached installer files in Homebrew's download directory are cleared during the cleanup phase.
  • iMessage — A text message is sent to your configured phone number via the macOS Messages app. This uses AppleScript and does not require any third-party services or API keys.
  • Log file — A detailed log is created at ~/Library/Logs/ recording every package that was updated, any errors, and the cleanup results.

Related Scripts

  • Auto-Update with Email & Text — An enhanced version of this script that also sends a detailed email report in addition to the text message summary.
  • Schedule Auto-Updates — Automates the creation of a macOS launchd agent so this script runs on a recurring schedule without manual intervention.