How to prevent your MacBook from sleeping when you close the lid

Screen Shot 2015-03-14 at 17.27.37

There’s a built-in command line tool in every Mac called caffeinate that prevents your computer from going to sleep, even when the lid is closed. This is the default behaviour if an external monitor is attached, but if that’s not the case, MacBooks just go to sleep as soon as you close the lid.

While several GUI tools are available (such as InsomniaX, or the Nosleep Extension, you can also call caffeinate from the command line without installing anything.

Open the Terminal app (under Applications – Utilities), and simply type

The cursor will disappear and your Mac won’t go to sleep. To terminate the behaviour, simply press CTRL+C – just like you would to stop any other shell command.

You can stop the command and close the Terminal session as soon as your lid is closed (and stays closed). If you open and close your lid again, your Mac will get sleepy again.

The command has a lot to offer, for example you could ask the hard disks from not sleeping using caffeinate -m, or prevent the display from going blank with caffeinate -d.

You can also specify a timeout using

This specifies the time in seconds you would like caffeinate to stay active (after 3600 seconds, or one hour, your Mac will sleep again).

Checkout man caffeinated from the command line for more options.

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

2 thoughts on “How to prevent your MacBook from sleeping when you close the lid

  1. nope, this has not worked in many years as far as i understand… this is false hope.
    Apple wants to baby their users, and stop them from stuffing a running laptop in their bookbag. Instead of, you know, using a temperature sensor.

    1. Hi Bob,

      not sure why you’ve got issues, the utility is working great for me – I’m currently using it with macOS Sierra 10.12.6 on a MacBook Pro 2011. I wouldn’t be writing about it if I hadn’t thoroughly tested the tool. I’ve not had a chance to test it under High Sierra yet.

Add your voice!