DEV Community

Cover image for Handle the Mac's sleep function from the terminal
Emanuele Bartolesi
Emanuele Bartolesi

Posted on • Originally published at

Handle the Mac's sleep function from the terminal

One of my favorite feature of my Mac is the optmized sleeping schedule to boost the battery life during the day.
But sometimes it's the worst feature in the world. :)
Especially in certain scenario like: downloading files, watching films, reading long articles and much more.

In the past I have used Caffeine but since a few versions of OSx there is a command line tool called Caffeinate with the same features.

Launch Caffeinate

As I mentioned before, Caffeinate, it's a command line utility, so you need a terminal application.
You can use the integrated terminal of the Mac or you can use other applications like Hyper.
Open you terminal tool, type caffeinate and hit return.

Alt Text

Now your Mac is under caffeine and doesn't go to sleep until you press "Ctrl+C" in the terminal.

Sleep after a while

The command has many parameters but the most useful, in my opinion, is the timer.
With the parameter -t the Mac stay awake for the value of the parameter and then the normal cycle of sleep/awake is resumed with the default behavior.

Alt Text

For instance, with the value 1800, the Mac sleeps after 30 minutes.

Other parameters

The tool has other parameters to interact more deeply with the system.
For example you can prevent the display from going to sleep but the other devices can remain with the default cycle.

-d prevent the display from going to sleep
-i prevent the system from idle sleeping
-m prevent the disk from going to sleep
-s prevent the machine from going to sleep if plugged in

Of course you can use these parameters in different ways and you can use multiple parameters in the same command.

Just an example:
caffeinate -d /Applications/Google\\ Chrome

This command prevent the display going to sleep if you are watching Netflix from Google Chrome.

Top comments (1)

dwiehoff profile image

How do you get your terminal to look that way (all black, no titlebar)?