Keeping Mountain Lion from Sleeping on the Job

iStockphotoA few weeks ago I began reformatting some video files on my aging iMac, and it was taking f-o-r-e-v-e-r. I queued up several files in HandBrake and went to bed, expecting to find the work mostly complete by morning. What I found was a stone cold snoozing iMac. I tapped on the keyboard to bring it from its slumber. The computer mocked me by displaying the “You didn’t disconnect your USB drive correctly” warning. The video processing had simply halted fifteen minutes after I went to bed the night before.

The release of Mountain Lion brought with it a change in the standard Mac OS X sleep behavior. In previous versions, application disk activity was sufficient to keep your machine awake; in Mountain Lion, each application must actively tell the OS not to sleep while it works. Apparently HandBrake doesn’t do this.

Here are two simple ways to keep your machine from stopping in the middle of the night shift.

The Caffeine App

This is the first approach I chose. It is the easiest to use, but your screen stays on all night.

If you are a Mac user, you probably have already installed this useful little app on your machine. If you don’t, slide on over to the Lighthead Software website and give it a try; it’s free.

It’s a little coffee cup that sits in the menu like this:

When you click it, your machine will stay fully awake for a configurable amount of time. I set it to indefinite.

It is the only utility I have ever installed on my wife’s Mac, and she uses it all of the time.

The only problem I have with using Caffeine to keep my Mac awake while it works is that everything stays awake. The screen stays on all night long. LCD monitors aren’t supposed to have any burn-in problems, but if you leave one on the same screen for a long time you definitely will see ghosts of your desktop icons when you are watching a video. Fortunately those ghosts fade, but I still don’t want the screen lit all night unnecessarily.

The Caffeinate Command

This is the technique I now use. It allows the screen to sleep during processing, but you need to use the command line for it to work.

When Apple made Mountain Lion, they intentionally changed the sleep behavior so that it will conserve batteries better. They want individual applications to create “assertions” that they need the computer to stay awake. Hopefully future application developers will build this kind of stuff into their apps, but when dealing with a cross-platform app such as HandBrake, it might not be so soon.

Apple provides a command-line utility called caffeinate that does the job for us.

There are two basic ways of using caffeinate, and you can read about them by typing man caffeinate at the terminal.

  • You can provide a fixed amount of time that you want your machine to stay awake.
  • You can add the caffeinate command to another command in order to stay awake until the other command finishes.

Rather than mess around with figuring out how many seconds were in 12 hours or even guessing how long the job would take, I decided to use the second approach, having caffeinate launch the HandBrake app.

Open a Terminal window (Spotlight “Terminal”) and type the following:

caffeinate /Applications/

That’s it!

This will cause HandBrake to launch, as if you had double-clicked on it. The difference is that your Mac will not go to a cold sleep until you exit HandBrake. The monitor will go off as scheduled, but the processor and disk will not sleep.

Now, all you need to do is configure your application to exit when it is done. I haven’t yet figured out how to get HandBrake to do that, but since I know it will be working hard all night long it doesn’t bother me that much.

In order to find the right path to provide to caffeinate, control-click on the application and choose Show Package Contents. You can then burrow down until you find the executable:

Note the path of enclosing folders shown at the bottom of the Finder window.


This new behavior will be seen as a good feature by some and an annoying bug by others, but at least Apple has provided a not-so-obvious way of keeping your work alive.

Perhaps the only serious flaw with either of these two techniques is that you have to remember to use them. On a couple of occasions I came back to find my Mac slumbering again, only to realize I had forgotten to turn on Caffeine.

If anyone has found a better way of doing this, let me know!

One Response to “Keeping Mountain Lion from Sleeping on the Job”

  1. Leland Gutierrez writes:

    thanks for the Caffeine tip !! my energy settings were set to 15mins for sleep (default). i’ve had my mac pro running vmware fusion (2 vm’s running) and coming back from lunch, my mac pro is quietly sleeping. I even had the Xcode update running overnight via the Mac App Store fail due to the mac pro going to sleep. today, with Caffeine, all is back to normal :) though I wish Apple would revert back to Lion sleep – as I prefer my vm’s to remain online and also time machine to finish its nightly backups before going to sleep.

Leave a Reply