DEV Community

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

Posted on • Originally published at dev.to

3 1

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.app/Contents/MacOS/Google\ Chrome https://netflix.com

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
dwiehoff profile image
Daniel

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

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay