In a lot of places in the world, many people still have to pay for bandwidth costs. I’m one of those people who just can’t afford to download lots of stuff during peak hours when my bandwidth might quickly get shaped or, worse, I’ll get charged. Nevertheless, there are often plenty of legit reasons to initiate huge downloads.
In these cases, it makes sense to be smart about when I initiate these downloads. Being something of a UNIX-head myself, I wanted to use the age-old
at command to download a Linux ISO during off-peak hours, which my ISP says starts at 2 AM. Much to my chagrin, I found that
at doesn’t work by default on Mac OS X and, worse, the Leopard man page leads to a dead end (though it didn’t back in Tiger…).
Turns out that the system daemon that is responsible for checking up on
at jobs has been wrapped with a
launchd job. This makes enabling
at on your system really easy:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist
Once you’ve done this, you can now use
at as you normally have done. For instance, I could now schedule my downloads to happen during the off-peak hours:
Perseus:Fedora maymay$ at 2:15am tomorrow # now press return curl -LO http://download.fedoraproject.org/pub/fedora/linux/releases/9/Fedora/x86_64/iso/Fedora-9-x86_64-DVD.iso # now press CTRL-D. job 1 at Tue Jul 15 02:15:00 2008 Perseus:Fedora maymay$ atq 1 Tue Jul 15 02:15:00 2008
This is also incredibly handy for scheduling just about any resource-intensive task that you don’t have to do right now. To take it one step further, you can even let the computer itself choose when to run these resource-heavy tasks by using the
batch command, which will execute commands much like
at but will check the system load average instead of the system clock to determine if it should start the job.
Note that with the
com.apple.atrun job loaded
/usr/libexec/atrun is started every 30 seconds (unless you change the
StartInterval key in the
plist file). Since the
atrun command checks a file on disk (that it places in the
/usr/lib/cron/jobs directory) to see if there is any work to do, this will probably prevent your disks from ever sleeping, which could be a major concern for battery life on portables. Also, obviously, your computer needs to be turned on and awake for the job to actually launch.
For more information, check out the result of typing
man at and
man launchctl at a Terminal prompt. There’s also a really good Google Tech Talk about Launchd that will teach you a lot more about job scheduling on Mac OS X.