Skip to content

Instantly share code, notes, and snippets.

@reavon
Forked from SimonSimCity/brew-update-notifier.sh
Last active October 6, 2023 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reavon/f116e31a64cd98292b1856cc65417df0 to your computer and use it in GitHub Desktop.
Save reavon/f116e31a64cd98292b1856cc65417df0 to your computer and use it in GitHub Desktop.
homebrew update notifier

launchd

First, open a Mac Terminal window, then cd to this directory:

$HOME/Library/LaunchAgents

When I dug around in the Apple documentation, I found there are three main directories you can use with launchd, and that's how I learned about this directory. Here are your three options:

  1. /Library/LaunchDaemons - Put your plist scripts in this folder if your job needs to run even when no users are logged in.
  2. /Library/LaunchAgents - Put your plist scripts in this folder if the job is only useful when users are logged in. (Note: I learned that this has the side-effect of your job being run as 'root' after a system reboot.)
  3. $HOME/Library/LaunchAgents - Put your plist files in this folder if the job is only useful when users are logged in. (When your plist configuration file is placed here, your job will be run under your username.)

Note that when you use the first two directories shown here, you must use the sudo command to edit your files.

To keep this simple and just see how things work initially, my advice is to use the $HOME/Library/LaunchAgents folder until you see how things work, then use the other two system folders if/when necessary.

#!/usr/bin/env bash
#
# Notify of Homebrew updates via Notification Center on Mac OS X
#
# Author: Chris Streeter http://www.chrisstreeter.com
# Requires: terminal-notifier. Install with:
# brew install terminal-notifier
TERM_APP='/Applications/Terminal.app'
BREW_EXEC='/opt/homebrew/bin/brew'
TERMINAL_NOTIFIER=`which terminal-notifier`
NOTIF_ARGS="-sender com.apple.Terminal"
$BREW_EXEC update 2>&1 > /dev/null
outdated=`$BREW_EXEC outdated --quiet | sed -e 's/.*\///'`
pinned=`$BREW_EXEC list --pinned`
# Remove pinned formulae from the list of outdated formulae
outdated=`comm -1 -3 <(echo "$pinned") <(echo "$outdated")`
if [ -z "$outdated" ] ; then
if [ -e $TERMINAL_NOTIFIER ]; then
# No updates available
$TERMINAL_NOTIFIER $NOTIF_ARGS \
-title "No Homebrew Updates Available" \
-message "No updates available yet for any homebrew packages."
fi
else
# We've got an outdated formula or two
# Nofity via Notification Center
if [ -e $TERMINAL_NOTIFIER ]; then
lc=$((`echo "$outdated" | wc -l`))
outdated=`echo "$outdated" | tail -$lc`
message=`echo "$outdated" | head -5`
if [ "$outdated" != "$message" ]; then
message="Some of the outdated formulae are:
$message"
else
message="The following formulae are outdated:
$message"
fi
# Send to the Nofication Center
$TERMINAL_NOTIFIER $NOTIF_ARGS \
-title "Homebrew Update(s) Available" -message "$message"
fi
fi
<?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>EnableGlobbing</key>
<false/>
<key>Label</key>
<string>homebrew.reavon.update-notifier</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/usr/local/bin/homebrew-update-notifier</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/homebrew.reavon.update-notifier.err</string>
<key>StandardOutPath</key>
<string>/tmp/homebrew.reavon.update-notifier.out</string>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>10</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<dict>
<key>Hour</key>
<integer>15</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</array>
</dict>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment