10.5: Schedule OS X software updates via script

Apr 09, '09 07:30:00AM

Contributed by: BjarneDM

This is something I came up with because of a support issue. I've now finally gotten my act together in sharing this with the world at large, inspired by Chris Breen's Scheduling OS X Software Update article at Macworld.com. Now, I'm the resident Mac Guru in the family, and for some associates. One of my female associates has a daughter who has borrowed my old decommissioned second-generation 800MHz 15" PowerPC iMac (you know -- the 'Luxo' model!). She is very happy with it, but I have some support issues as:

  1. Their ISP blocks every outside access, including ssh and vnc
  2. The computer is so old that screen sharing through iChat doesn't work -- that requires at least 64MB on the graphics board, and this model iMac has only 32MB.
Updating Mac OS X Leopard remotely is thus somewhat problematic, so I came up with the idea of automating the Software Update by installing a LaunchDaemon and an associated script. Now, I just have to tell her not to turn the computer off when she goes to bed in the evening, and my script does what's needed.

This is the LaunchDaemon plist file; just save it as info.mathiesen.softwareupdate.plist in /Library/LaunchDaemons. You can, of course, adjust the schedule according to your own wishes.

<?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>Label</key>
    <string>info.mathiesen.softwareupdate</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/sbin/softwareupdate</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Minute</key>
        <integer>0</integer>
        <key>Hour</key>
        <integer>2</integer>
    </dict>
</dict>
</plist>
Here is the script that does the dirty work. Put it in /usr/local/sbin, or somewhere else on your path. If you store it elsewhere, change the path in the above plist file to point to the new location. Remember to chmod +x the script file, so it can run.
#!/bin/bash

rm /Library/Caches/*
/usr/sbin/softwareupdate --install --all && reboot
Some explanation might be in order. The reason for the rm /Library/Caches/* is that on all of my PopwerPC computers (I've got three including the iMac), the sound goes completely haywire when iTunes has been updated if those caches haven't been deleted. Clearing the caches completely resolves the sound problem.

Some might ask "Why not just put all of the Software Updates in the plist and do away with the bash script?" There are two reasons for that:
  1. softwareupdate executed in a script or in the Terminal isn't able to reboot the computer by itself like the GUI version is
  2. I need to clear the caches
[robg adds: I haven't tested this one. An alternative solution is presented in this hint.]

Comments (8)


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