MacPort Package Update Notifications via Growl

I wrote a quick little script this morning to notify myself of updates to MacPorts via the Mac OS X notification app Growl.  The script is a bash script that is designed to run as root from a cronjob (as the port sync command requires root permissions). You can find the script committed in my dotfiles repository on github.  For convenience, I pasted the script below, though the copy on github will always be the most up-to-date copy.

#!/bin/bash
#
# Notify of MacPort updates via Growl on Mac OS X
#
# Author: Chris Streeter http://www.chrisstreeter.com
# Requires: Growl Notify Extra to be installed (but fails gracefully). Info
#       about how to get the extra is at http://growl.info/extras.php
 
TERM_APP='/Applications/Terminal.app'
PORT_EXEC='/opt/local/bin/port'
GROWL_NOTIFY='/usr/local/bin/growlnotify'
GROWL_TITLE="MacPort Update(s) Available"
GROWL_ARGS="-n 'MacPorts' -d $GROWL_NOTIFY -a $PORT_EXEC"
 
$PORT_EXEC sync 2>&1 > /dev/null
outdated=`$PORT_EXEC outdated`
 
if echo $outdated | grep -q 'No installed ports are outdated.' ; then
    if [ -e $GROWL_NOTIFY ]; then
        # No updates available
        $GROWL_NOTIFY $GROWL_ARGS -m '' -t "No MacPort Updates Available"
    fi
else
    # We've got an outdated port or two
 
    # Nofity via growl
    if [ -e $GROWL_NOTIFY ]; then
        lc=$((`echo "$outdated" | wc -l` - 1))
        outdated=`echo "$outdated" | tail -$lc | cut -d " " -f 1`
        message=`echo "$outdated" | head -5`
        if [ "$outdated" != "$message" ]; then
            message="Some of the outdated packages are:
$message"
        else
            message="The following packages are outdated:
$message"
        fi
        # Send to growlnotify
        echo "$message" | $GROWL_NOTIFY $GROWL_ARGS -s -t $GROWL_TITLE
    fi
fi

Assuming the script is at ~/bin/port-update-notifier, you can install the script to a crontab by running sudo crontab -e, then adding the line 0 12 * * * /Users/<username>/bin/port-update-notifier to the end of the file (substituting <username> for your username, or wherever you’ve put the script). I’ve chosen to run the script every day at noon because my computer is likely to be on and connected to a network.

  • Andrew Davidson

    Great idea. I’ve been using Homebrew rather than MacPorts but I plan to fork and modify this as soon as I get back to my machine

  • http://www.chrisstreeter.com Streeter

    Andrew, what made you choose Homebrew over MacPorts?

  • http://amdavidson.com Andrew Davidson

    I like homebrew’s use of /usr/local.

    It makes things a little easier when developing scripts for my VPS on my local machine.