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.
Share
Categories
In response to a Macworld article, TidBits' Chris Pepper elaborated on ways to run Software Update, Apple's means of delivering updates and...
Add a Comment
Wait...am I missing something? Wouldn't you want automatic updates to run daily at, say, 3am? After all, the updates applied can be controlled with MCX settings that point the clients to a local software update server, meaning only approved updates are applied. Windows Group Policy does this seamlessly, allowing me to manage 1,000+ lab computers without manually updating them. Am I really hearing correctly here that I should touch each Apple machine to be safe? Any particular reason for that--i.e. has anyone had any problems with updates being automatically applied as long as they're sifted through a local server first?
January 15 2010 at 5:01 PM Report abuse Permalink rate up rate down Replypslatt: you probably downloaded 1.2, since that's the biggest shiniest download button on the linked page (http://sourceforge.net/projects/lingon/files/ ) - I did exactly the same, and was surprised to find that running "Check For Updates" (in the Lingon menu, I think) detected a newer version and downloaded it. You can download it yourself from that page though - you want Lingon-2.1.1.zip/download :-)
January 12 2010 at 8:54 AM Report abuse Permalink rate up rate down ReplyI've been messing around with this problem for some time; I've got a scripted solution in part based upon the launchd version of periodic.daily - meaning that I add a script to those executed by periodic.daily. The problem I haven't solved completely yet is : how do I advise the users that the computer is about to install softwareupdates in 15 minutes.
It does solve one of my other problems: I'm the resident troubleshooter for several relatives and other associates wiht macs. The problem with softwareupdate is that it doesn't run if the user isn't an administrator, and all of the persons I'm supporting are running as normal users due to security issues. The second problem with softwareupdate is that sometimes it's required to close programs in order for the updates to install.
So far my solution looks like this:
#!/usr/bin/bash
declare -i updates=$( softwareupdate -l 2>/dev/null | sed -nE '/ * /p' | wc -l )
declare -i restart=$( softwareupdate -l 2>/dev/null | sed -nE '/[restart]/p' | wc -l )
cd /Volumes/Bjarne/WebServer/scripts
if [[ ${updates} -gt 0 ]]
then
open -n -a /Applications/Safari.app warning.html
sleep $(( 15*60 ))
ps -ajx
| awk '/loginwindow/ && !/awk/ {print $2}'
| xargs -n1 kill -KILL
softwareupdate -i -a 2>/dev/null
fi
if [[ ${restart} -gt 0 ]]
then
reboot
fi
When I just run this script from Terminal the warning message pops up as expected but when run as part of periodic.daily the warning doesn't appear :-(
To break it down:
1) discover if there are any softwareupdates
2) issue a warning
3) log all users out
4) install the updates
5) possibly : reboot if needed
To make it run I create a symbolic link to the script in /etc/periodic/daily
The version of Lingon I downloaded is very different that the one in the article. I could still implement script though. Otherwise this is a great article.
January 11 2010 at 7:24 AM Report abuse Permalink rate up rate down ReplyBrett,
You should really write more for TUAW. You write very well, the topic is interesting, and it's refreshingly not about you (Oh, how many TUAW articles start "If you're like me, ..." Drives me nuts sometimes.)
So anyway, thanks for this. Good stuff.
Every 15 minutes, really? You can set Software Update to run daily (at least on Snow Leopard), shouldn't that be enough?
Also, before anyone tries to do it, don't do "softwareupdate -i -a" in your command to automatically install all available updates. That's just asking for system instability.
One would hope that daily would suffice, but you apparently haven't met the more manic segment of OS X fans. :)
Agreed, a fully-automated background install would be asking for trouble. The idea behind a full download is to force the downloads into a timeframe better suited to a users bandwidth allowances, allowing them to install updates already retrieved. Any `softwareupdate` command run more frequently should just be listing available downloads.
Hot Apps on TUAW
Deals of the Day
more deals- Wicked Jaw Breaker Noise-Isolating In-Ear Headphones for $6 + free shipping
- Refurb Apple MacBook Air Laptops: 12" 64GB SSD for $699 + free shipping
- JVC Motion Sensing Clock Radio with Dual iPod Docks for $55 + free shipping
- Apple iPhone Headset with Mic for $4 + $2 s&h
- Refurb Apple iPod nano 8GB MP3 Player for $99 + free shipping, 16GB for $119
- Hannspree Apple-Shaped 28" 1080p LCD HDTV for $270 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



7 Comments