Skip to content

Instantly share code, notes, and snippets.

@ErnHem
Forked from jb510/ReadMe.md
Last active February 16, 2025 10:52
Show Gist options
  • Save ErnHem/0db5c6d3f372166715b26331865df93a to your computer and use it in GitHub Desktop.
Save ErnHem/0db5c6d3f372166715b26331865df93a to your computer and use it in GitHub Desktop.
Auto Update Brew: OS X Launchd job and script to automatically update homebrew
  1. Place homebrewupdate.sh where ever you like, I use ~/.bin/homebrewupdate.sh
  2. Place the .plist file in ~/Library/LaunchAgents and update the path to your bash script
  3. run launchctl load ~/Library/LaunchAgents/homebrew-upgrade.plist or whatever you've named your plist file.
  4. check launchctl list | grep homebrew to see that it's running

Note: You might need to chmod +x homebrewupdate.sh

Note 2: you can run this manually to test your script ~/.bin/homebrewupdate.sh

Future: Update this to also update casks https://github.com/buo/homebrew-cask-upgrade or using brew cask list | xargs brew cask reinstall

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>$PATH:/Users/nkunashko/.bin</string>
</dict>
<key>Label</key>
<string>homebrewupdate</string>
<key>LowPriorityIO</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>/Users/nkunashko/.bin/homebrew-upgrade.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/nkunashko/.bin/homebrew-upgrade.log</string>
<key>StandardOutPath</key>
<string>/Users/nkunashko/.bin/homebrew-upgrade.log</string>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>
#!/bin/bash
#
# Notify of Homebrew updates via Notification Center on Mac OS X
# Requires: terminal-notifier. Install with:
# brew install terminal-notifier
BREW_EXEC='/opt/homebrew/bin/brew'
TERMINAL_NOTIFIER='/opt/homebrew/bin/terminal-notifier'
NOTIF_ARGS="-activate com.apple.Terminal"
GIT='/usr/bin/git'
TR='/usr/bin/tr'
AUTO=true
NONINTERACTIVE=1
# update brew itself
cd "$($BREW_EXEC --repo)" && $GIT fetch && $GIT reset --hard origin/master && $BREW_EXEC update
{ $BREW_EXEC update > /dev/null; } 2>&1
outdated=$($BREW_EXEC outdated | $TR ' ' '\n')
outdated_cask=$($BREW_EXEC outdated --cask | $TR ' ' '\n')
if [ -n "$outdated" ] && [ -n "$outdated_cask" ] ; then
# Nofity via Notification Center
if [ -e "$TERMINAL_NOTIFIER" ]; then
message="The following formulae are outdated:"$'\n'"$outdated"$'\n'"$outdated_cask"
# Send to the Nofication Center
$TERMINAL_NOTIFIER "$NOTIF_ARGS" -title "Homebrew Update(s) Available" -message "$message"
fi
# Automatically update
if ( $AUTO ); then
for app in ${outdated_cask//\\n/}
do
if $BREW_EXEC upgrade --cask "$app"; then
$TERMINAL_NOTIFIER "$NOTIF_ARGS" -title "Homebrew Updater" -message "The $app cask app was successfully updated"
else
$TERMINAL_NOTIFIER "$NOTIF_ARGS" -title "Homebrew Updater" -message "Something went wrong with the $app cask app"
fi
done
for app in ${outdated//\\n/}
do
if $BREW_EXEC upgrade "$app"; then
$TERMINAL_NOTIFIER "$NOTIF_ARGS" -title "Homebrew Updater" -message "The $app app was successfully updated"
else
$TERMINAL_NOTIFIER "$NOTIF_ARGS" -title "Homebrew Updater" -message "Something went wrong with the $app app"
fi
done
if $BREW_EXEC doctor; then
$TERMINAL_NOTIFIER "$NOTIF_ARGS" -title "Homebrew Updater" -message "Doctor found some issues pls run 'brew doctor'"
fi
if $BREW_EXEC missing; then
$TERMINAL_NOTIFIER "$NOTIF_ARGS" -title "Homebrew Updater" -message "Some apps have missing dependencies pls run 'brew missing'"
fi
$BREW_EXEC cleanup -s
fi
fi
# Install Homebrew.
# Install terminal-notifier. Execute brew install terminal-notifier.
# copy the homebrew-upgrade.sh file to /usr/local/bin don't forget about chmod +x
# Copy the homebrew-upgrade.plist file in this gist into ~/Library/LaunchAgents/.
# Execute launchctl load ~/Library/LaunchAgents/homebrew-upgrade.plist. Do not run this command with superuser privileges.
# ~/Library/LaunchAgents/homebrew-upgrade.plist
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
# <plist version="1.0">
# <dict>
# <key>Disabled</key>
# <false/>
# <key>EnvironmentVariables</key>
# <dict>
# <key>PATH</key>
# <string>$PATH:/Users/nkunashko/.bin</string>
# </dict>
# <key>Label</key>
# <string>homebrewupdate</string>
# <key>LowPriorityIO</key>
# <true/>
# <key>ProgramArguments</key>
# <array>
# <string>/bin/bash</string>
# <string>-c</string>
# <string>homebrew-upgrade.sh</string>
# </array>
# <key>RunAtLoad</key>
# <true/>
# <key>StandardErrorPath</key>
# <string>/Users/nkunashko/.bin/homebrew-upgrade.log</string>
# <key>StandardOutPath</key>
# <string>/Users/nkunashko/.bin/homebrew-upgrade.log</string>
# <key>StartInterval</key>
# <integer>60</integer>
# </dict>
# </plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment