7

I'm confused about the differences among the three (that I know of) tools for scheduling running of code on OS X / Darwin:

I understand that cron is largely deprecated, but it is still used by some utilities (like Super Super!, for some reason), and that pmset has the ability to wake the computer to run specific jobs. But I'm not clear what would guide the selection of one over the other in general.

How do these three methods OS scheduling differ (especially on Mavericks, if things are different there), especially in the kids of tasks they are suited to, and how they behave during sleep or hibernation?

5

Use launchd instead of cron, at, or other legacy UNIX tools for Mac OS X.

pmset

pmset is a built-in command line tool for getting and setting power related values on Mac OS X. One of pmset's abilities is to add and remove entries from the IOPMQueue; the IOPMQueue is the IOKit level queue that allows your Mac to power on and wake up at a specific time. IOPMQueue also supports scheduling of shut downs and restarts.

Arbitrary code can not be scheduled using pmset or through the IOPMQueue.

cron

cron is a stalwart of the task scheduling world. However on Mac OS X, using cron is not recommended. Use launchd.

cron is implemented by checking each minute for tasks that are due. This allows for a powerful and expressive scheduling format but at the cost of needing to wake the processor up every minute.

cron does not know about your Mac sleeping. If a task is due while your Mac is asleep or powered off, the task will not occur. When your Mac powers back on, the task will not be performed because the minute in which it should have run has passed.

There are other tools that build upon cron and can run missed tasks upon waking. None are included by default with Mac OS X.

launchd

launchd (launch daemon) is Apple's system starter, scheduling tool, and process watchdog. launchd is written for the needs of the operating system first and users second. It is a powerful and capable process used to manage or launch almost every other process on your Mac.

launchd is an improvement on cron for scheduling because it does not need to poll to perform tasks. Instead launchd can remain inactive until the moment a task needs performing. In an era of energy aware and mobile computing, this is important.

launchd will not wake a sleeping Mac to perform tasks.

launchd job tickets are property list (plist) files. These files can be edited with a text editor or with a number of third party applications.

launchd is recommended for, and is suitable for, most scheduling situations.

Third Party

There are number of third party scheduling tools available on Mac OS X. They often provide a graphical interface or offer more features than are included in the standard Mac OS X command line tools. The tool I am associated with is Power Manager.

  • This all makes sense, but why would a product (Super Duper!, for example) continue to use cron, especially if it is waking the processor every minute?! – orome Dec 2 '13 at 14:11
  • Shirt Pocket's use of cron for Super Duper! is not bad but instead likely some design constraint in their plans, or a limitation in launchd that they encountered during development. There is little incentive for Shirt Pocket to migrate to launchd so long as cron continues to work well for their customers. – Graham Miln Dec 2 '13 at 14:22
  • 1
    Yes, cron sits in a while loop checking crontabs once per minute. The cron code used in OS X 10.9 is open source: opensource.apple.com/source/cron/cron-39/cron/cron.c look for the cron_tick function and calling loop to confirm this behaviour. – Graham Miln Dec 2 '13 at 14:38
  • 1
    So cron wakes every second even of there are no scheduled cron tasks — i.e., the waking would go on even if there were none? – orome Dec 2 '13 at 15:19
  • 1
    cron wakes every minute to check if any tasks are due. – Graham Miln Dec 2 '13 at 15:38
3

pmset is low level stuff, and doesn't really control running of code specifically, at least not in any direct or intended way. It controls things like turning your Mac on or off at specific times, similar to the way that you can do in System Preferences » Energy Saver. Generally speaking, you shouldn't ever need to use pmset for anything directly.

cron is still there because… well, because Apple hasn't killed it off yet, despite having deprecated it ages ago. I suspect it’s because there are a few old-time Un*x folks still working at Apple who don't want to see it go. But, really, there's no real sense in using that if you can use launchd. IIRC cron jobs do not run if the computer was asleep when it was supposed to run.

launchd is Apple's replacement for cron which adds a lot of flexibility to the scheduling of events on a Mac. This is the way that you should be scheduling events on a Mac. The best way to learn how to use launchd is LaunchControl, which is a US$10 app with no DRM, no license codes, and no restrictions. (The developers rely on the honesty of their users to pay the registration fee if they use the app.) Not only do you get drag-and-drop functionality of bringing in various elements of launchd plists, each one is documented so you can see exactly what it does. Highly recommended.

  • Regarding pmset, how does an app like Power Manager work? It clearly creates pmset entries (and does not use cron or launchd) and also does more than simply wake the machine (it runs arbitrary code). – orome Dec 1 '13 at 19:04
  • That I don't know. – TJ Luoma Dec 1 '13 at 20:58
  • Power Manager works directly with IOPMQueue to power on your Mac. Power Manager uses its own scheduling engine to run scheduled actions. – Graham Miln Dec 2 '13 at 11:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .