Advertisement

Manually schedule Software Update 'the OS X way' with launchd

In response to a Macworld article, TidBits' Chris Pepper elaborated on ways to run Software Update, Apple's means of delivering updates and patches, on your own schedule. Beginning with the fact that Software Update schedules its next update based on the time it's currently being run, setting the time for the next update is as easy as running it manually at the time you want it to be scheduled for in the future.

Later, Pepper delves into the command line method of updating, using the softwareupdate tool (which we've talked about on TUAW, too) to run it from Terminal. Taking that a step further, it's suggested that you run the command from cron, a UNIX command for scheduling tasks, to automate the command-line updates. However, while it still works fine and is perfectly capable of the task, cron has technically been deprecated in OS X since Tiger. I thought I'd mention the newfangled "Mac OS X way" of handling scheduled tasks, and demonstrate a little of its flexibility.

Launchd is Apple's replacement for several UNIX ways of doing things, including init, rc.d scripts and cron. It provides a uniform, XML configuration method and -- in many cases -- is more secure than the replaced methods. Launchd can trigger applications and scripts at boot time, at intervals or even when a file or the contents of a folder change. It can also make sure a daemon or an application keeps running, with the ability to respawn and throttle it. If that's just a bunch of nerd-speak to you, don't worry, this isn't going to be an overly technical post. You can read more specifics about launchd on Apple's developer site, if you want more geeky goodness.

Launchd configuration files, like much of OS X, are XML files. Each process has one, and they can exist just about anywhere. A tool called launchctl is used to add and remove them from launchd. While these files are technically human-readable, they're not the most fun to create and edit. In the interest of keeping this as non-technical as possible, I'm going to use a very handy utility called Lingon. The latest version (2.1.1) can be found at Sourceforge. It's no longer under active development, but it's working fine in Leopard and Snow Leopard. Grab a copy, put it in either your Applications folder or into Applications/Utilities, and launch it.


You'll see all of your existing daemons and agents in Lingon's sidbar. Unless you know exactly what you're doing, you'll generally only want to edit/add scripts in the "My Agents" section to avoid breaking anything at the system level. Create a new script using the plus button in the upper left, and name it something unique in section 1 of the edit area; I prefix my launchd scripts with my own name, e.g. com.brettterpstra.awesomescript, but anything will work.

Section 2 is where our command goes. In this case, we're running the softwareupdate command, and we want it to automatically download any available updates in the background. We'll use /usr/sbin/softwareupdate --download --all in that field.

Section 3 gives us the various options for running the command. In Lingon's interface they're pretty self-explanatory, so I won't go through each one. We'll just use "At a specific date:," "Every Day," and whatever time works best for you (and your bandwidth allowances). Make sure the enabled checkbox in the upper right is checked, hit the save button at the top, and you've got your own Software Update scheduler. Change the time at will, or use one of the other options to control how often it runs. If you enable this, you'll probably want to disable the automatic checking in Software Updates panel in System Preferences.

There are other possibilities, too. For example, if you wanted to be notified as soon as possible about available updates, you could write a script that ran softwareupdate with the "-l" option (to list available updates without downloading them), parse the output and have it send you an email or a direct message if it found any updates. Run it about every 15 minutes and you could be among the first to know about an update! You can also use the launchd manager (launchctl), or Lingon to disable background processes that other programs have added, but that you don't want running. Whether you're a UNIX user still hanging on to cron, or are just a regular user who wants something besides iCal for scheduling scripts and launching applications, this will hopefully get you started with the 'new' OS X way of doing it.