Disable Sleep on a Mac from the Command Line with caffeinate

Aug 3, 2012 - 20 Comments

Caffeinate causes a Mac to avoid sleeping via command line

You’ve always been able to temporarily prevent a Mac from sleeping by using the pmset noidle command or a hot corner, but with modern versions of Mac OS X, Apple has bundled a command line tool dedicated to sleep prevention much like the popular Caffeine app, and appropriately, named it caffeinate.

At it’s most simple usage, caffeinate just prevents sleep entirely, but you can modify the command with various flags to prevent just the display from sleeping, provide a specified time to avoid sleeping, prevent sleeping while a command runs, and more. A few useful examples are discussed below.

How to Disable Sleep with the ‘caffeinate’ Command on Mac

To temporarily disable sleep functions regardless of sleep settings, run the following command from Terminal in MacOS / Mac OS X:

caffeinate

At the core basic function of the command, caffeinate is all that’s necessary, and while caffeinate is active sleep will be prevented until it’s no longer running.

To stop caffeinate and return to normal sleep behavior, you can hit “Control+C” to quit out of caffeinate as it’s running like this, or you can kill it with the ‘killall caffeinate’ command if desired.

You can also run caffeinate for a pre-determined amount of time to prevent sleeping for a specified block of time, say for 4 hours while you download something, and then run it in the background by adding & to it:

caffeinate -t 144000 &

The number attached to the -t flag is the amount of time in seconds for sleep to be disabled on the Mac.

Caffeinate can also be attached to other commands to prevent the Mac from sleeping while that given command runs:

caffeinate [command_to_run] -arguments

That causes the Mac to avoid sleep only as long as it takes to run the command provided, after which the normal sleep rules apply.

With these latter tricks, if you run caffeinate in the background and want to cancel it then the easiest way to quit the process is to issue the kill command for the reference process ID, or simply ‘killall caffeinate’

To use the caffeinate command you’ll need to be running a fairly modern version of Mac OS X, as the feature was introduced in 10.8 Mountain Lion and persists through 10.9 Mavericks, and into 10.10 Yosemite, macOS Big Sur, macOS Ventura, and beyond.

Heads up to MacWorld for the tip

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

20 Comments

» Comments RSS Feed

  1. jw says:

    FWIW- the caffeinate binary is working great for me in High Sierra to remotely wake a screen at the login window (via ssh, Apple Remote Desktop and even in a LaunchDaemon). To be specific I’m using the command like so:

    /usr/bin/caffeinate -disu -t 5

    Hope this helps somebody!

  2. Debra says:

    Both “caffeinate” and “pmset noidle” SAY they’re working — as in, when I close the Terminal, I’m warned that I will be canceling the command — but both FAIL to do what I’m trying to do: Close my screen and use my laptop as a music player, with 8tracks.com still running. What am I doing wrong?

    • Warren says:

      Preceed the command with “nohup” and it will detach completely from your current session so that when your terminal session ends, caffeinate will still be running.

      nohup caffeinate &

      • Idan says:

        This is not working in macOS Sierra 10.12.1.

        Not from command line
        Not the Caffeine app
        Not the KeepMeAwake app
        Not InsomniaX

        Nothing!
        Is this broken in macOS Sierra?

  3. Luke says:

    Isn’t the time in the example 40 hours and not 4 hours

    4 hours in seconds is: 14,400

    40 hours in seconds is: 144,000

    Thanks,

    Luke

  4. Mteacher says:

    The current build of Caffeine doesn’t work properly under Mountain Lion – ML’s set sleep parameters override Caffeine’s capabilities in some instances.

  5. Jay says:

    there is a much simpler command with nothing being required to be installed.

    “pmset noidle” <<< as long as this terminal session is running, your computer will not sleep.

    • wolfgang says:

      noidle – pmset prevents idle sleep by creating a PM assertion to prevent
      idle sleep(while running; hit ctrl-c to cancel). This argument is
      deprecated in favor of caffeinate(8). Please use caffeinate(8) instead.

  6. Vlad Dj Kax says:

    One thing that doesn’t seem to work with Caffeine (unless I’ve made a mistake..) is that, once you lock your session, it seems to stop preventing the Mac from sleeping …

    • Aberto I says:

      It is what I was asking above: “And how to turn to a normal situation?'”

      What is the terminal instruction?
      decaffeinate?? :D

      • MacBookPro:~ yapchinhoong$ caffeinate &
        [1] 324
        MacBookPro:~ yapchinhoong$ ps -ef | grep caffeinate
        501 324 306 0 11:05PM ttys001 0:00.00 caffeinate
        501 326 306 0 11:05PM ttys001 0:00.00 grep caffeinate
        MacBookPro:~ yapchinhoong$
        MacBookPro:~ yapchinhoong$ kill 324
        501 330 306 0 11:05PM ttys001 0:00.00 grep caffeinate
        MacBookPro:~ yapchinhoong$

        • MacBookPro:~ yapchinhoong$ caffeinate &
          [1] 324
          MacBookPro:~ yapchinhoong$ ps -ef | grep caffeinate
          501 324 306 0 11:05PM ttys001 0:00.00 caffeinate
          501 326 306 0 11:05PM ttys001 0:00.00 grep caffeinate
          MacBookPro:~ yapchinhoong$
          MacBookPro:~ yapchinhoong$ kill 324
          MacBookPro:~ yapchinhoong$ ps -ef | grep caffeinate
          501 330 306 0 11:05PM ttys001 0:00.00 grep caffeinate
          MacBookPro:~ yapchinhoong$

    • Kiwiiano says:

      I suspect you are mistaken, VDK. If I toggle Caffeine on, it stays on indefinitely, although this MBP blacks out if I move the cursor into the top right corner or if I close the lid. By control clicking the menubar icon I also have the options to stay awake for 5, 10, 15, 30, 60 or 120 minutes. Beats messing around with Command lines.

  7. Vlad Dj Kax says:

    Or.. you can download Caffeine from the Mac App Store..

    http://itunes.apple.com/us/app/caffeine/id411246225?mt=12

  8. Aberto I says:

    “To temporarily disable sleep, run this command from Terminal: caffeinate”

    Temporarily… How much time?
    And how to turn to a normal situation?

    • Will says:

      Sleep is prevented for as long as caffeinate runs, if you stop caffeinate or kill the process it will return to normal behavior. Likewise, if you set a time limit it will only prevent sleep for the specified time before returning to normal behavior.

    • Daizy says:

      β€œTo temporarily disable sleep, run this command from Terminal: caffeinate” Temporarily… How much time? And how to turn to a normal situation?

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site