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: the script compares your current network name against WIFI_NETWORK, which is set at the top of the script. This keeps large downloads off a metered hotspot or public Wi-Fi. If the network does not match, the script texts you a "Skipped" notice, logs the reason and exits without updating.
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. AC power is required: if your Mac is on battery, the update is skipped until the next scheduled run.
Once the pre-flight checks pass, the script runs four Homebrew operations in sequence: brew update, brew upgrade, brew upgrade --cask and brew cleanup. 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, brew upgrade and brew upgrade --cask install newer versions of outdated formulae and apps, with their output written to the log. Finally, brew cleanup, which removes old versions of packages that are no longer needed and clears the download cache to free up disk space.
When the operations finish, the script builds a short summary: the time, the number of successful operations, the number of errors, and whether everything succeeded. 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.