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.