A script to install all required Software Updates

Sep 19, '08 07:30:04AM

Contributed by: phillymjs

After a version of OS X matures, it can be a pain to sit through several rounds of updates if you need to rebuild a machine from scratch. You install one set of updates, the machine restarts, and then the next set of updates pops-up. After a few cycles, this gets really old.

To work around that problem, I created a shell script that runs Software Update, installs all available updates, reboots, and repeats the process until there are no more updates left.

#!/bin/sh
# This script will run softwareupdate, install all available updates, reboot and repeat
# until no more updates are available. It must be run as root.
# To use in its current form, name the script initswupdater.sh and put it in /Library/Management.
# Tested in both Tiger and Leopard.

if [ -e /Users/Shared/.initswupd_inprog ]
then
    # If the 'updates in progress' marker is there, run the updates.
    # Temporarily prevent machine from sleeping.
    pmset -a sleep 0 force
    # Install all available software updates.
    softwareupdate -ai
    # Run softwareupdate again to see if there's anything left.
    # Softwareupdate returns 3 lines if there are no updates.
    if [ `softwareupdate -l | wc -l` -le 3 ]
    then
        # If there are no more updates available, clean up the marker, launchdaemon and login window text.
        rm /Users/Shared/.initswupd_inprog
        rm /Library/LaunchDaemons/com.management.initswupdater.plist
        defaults delete /Library/Preferences/com.apple.loginwindow LoginwindowText
    fi
else
    # If the 'updates in progress' marker is not there, prep the machine.
    # Create the marker file so the script knows to keep going.
    touch /Users/Shared/.initswupd_inprog
    #Set the loginwindow banner to warn people not to use the machine.
    defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText "Software updates are currently being installed on this computer. Please do not attempt to log in until this message is gone."
    #Put the daemon in the LaunchDaemons folder, so the script runs again after reboot.
    plistfile='<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0">\n<dict>\n<key>Label</key>\n<string>com.management.initswupdater</string>\n<key>ProgramArguments</key>\n<array>\n<string>/bin/sh</string><string>/Library/Management/initswupdater.sh</string>\n</array>\n<key>RunAtLoad</key>\n<true/>\n</dict>\n</plist>'
    # Writing the LaunchDaemon plist file must be done differently in Tiger than Leopard
    osversionlong=`sw_vers -productVersion`
    osvers=${osversionlong:3:1}
    if [ $osvers -eq 4 ]
    then
        # Tiger
        echo -e $plistfile > "/Library/LaunchDaemons/com.management.initswupdater.plist"
    else
        # Leopard
        echo $plistfile > "/Library/LaunchDaemons/com.management.initswupdater.plist"
    fi
fi
# Reboot
shutdown -r now
[robg adds: I haven't tested this one yet, but I've got it marked for the next time I go through this process. To make it work, remember to make it executable: chmod a+x initswupdater.sh. Update: Please read the comments for more-thorough installation and usage instructions.]

Comments (33)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080916220249270