In the Windows world, system administrators are likely to be familiar with the Scheduled Tasks service. This allows you to schedule processes, scripts or applications to run regularly at a specific time. In the Linux (and UNIX in general) world the cron daemon has traditionally handled this task.
Until the Tiger release, OS X also used the cron system. However in Tiger and Leopard, Apple replaced it with launchd. This does more than just task scheduling; its job is to launch processes, scheduled or otherwise, however the focus on this article is using it for scheduling tasks.
Why would you want to schedule a task? I have three custom scripts that provide examples:
- Synchronizing my Twitter status to my IM status. It does this every half hour.
- Putting a saved audio file on a remote server into iTunes. This runs at 8AM every Friday.
- Running a customized backup script that runs every day at 8AM.
While launchd is configured through text plist files, to set up basic scheduling we don’t need to know how these work. All we need is a free utility called Lingon.
Once loaded, Lingon displays a list of scheduled events grouped by their scope. It is important to know what these mean so the right section can be chosen when adding a schedule.
- My Agents: These only apply when you are logged in.
- Users Agents: These apply to all users, but only when they are logged in.
- Users Daemons: These apply all the time, even when no user is logged in.
- System Agents: These apply to all users and are necessary operating system services. These are maintained by Apple and should not be modified.
- System Daemons: These entries apply all the time, even when no user is logged in and are necessary operating system services. These are maintained by Apple and should not be modified.
To create a new scheduled event, simply click the new button and select which section your task needs to be in. Enter the name of the process, this can be anything, but should really consist of a unique name such as ‘com.appleblog.example-schedule’. Choose the application or script you want to run, and then specify when you would like it to run.
At this stage you can click save and log back in and out of your user session to make your new settings take hold. If your Mac is sleeping at the scheduled time a task should run, this task will run when you wake the machine up.
This is all you need to do for basic schedules. If you do need to do fancy configuration, Lingon allows almost complete control of your tasks beyond its simple UI. Pressing the “Expert Mode” button at the bottom will allow you directly edit the text plist file, easily entering parameters by a drop down list at the top. For full documentation on the launchd plist files, see the launchd.plist man page.
If you want even more control, such as permanently deleting displayed entries, you can delete the physical configuration files. These can be found in the following locations:
- My Agents: /Users/username/Library/LaunchAgents/
- Users Agents: /Library/LaunchAgents/
- Users Daemons: /Library/LaunchDaemons/
- System Agents: /System/Library/LaunchAgents/
- System Daemons: /Users/bed/Library/LaunchDaemons/
Warning: Be very careful with System Agents and Daemons, as these provide crucial elements of OS X. These should only be viewed to gain a better understanding of how OS X works. You are likely to break your operating system if you modify them.