How do I use caffeinate to prevent my computer from sleeping while a script runs?

12,653

-s option is commonly used with laptops (If the lid is closed, the system does to forced sleep). -s only applies when not using a battery - if there is limited power in the battery the system will sleep no matter what.

-i is for idle sleep which happens when the system has not been used for a certain period.

Note you can use both options keep the system perky if required:

caffeinate -is python spam.py

From the Mac developer library, the differences between idle sleep and (forced) sleep are:

  • Forced sleep occurs when the user takes some sort of direct action to cause the machine to sleep. Closing the lid on a laptop or selecting sleep from the Apple menu both cause forced sleep. The system will also induce forced sleep under certain conditions, for example, a thermal emergency or a low battery.

  • Idle sleep occurs when the machine is unused for a specific period of time configured in the Energy Saver System Preferences

Share:
12,653

Related videos on Youtube

user12345678
Author by

user12345678

Updated on September 18, 2022

Comments

  • user12345678
    user12345678 over 1 year

    This morning I started a very long running script with:

    caffeinate python spam.py
    

    and when I checked in on the computer several hours later, the computer was asleep! I moved the mouse and the script resumed executing normally. I've looked at man caffeinate and it looks like I should actually use:

    caffeinate -s python spam.py 
    

    or

    caffeinate -i python spam.py
    

    What do I need to take into account when picking between -s and -i? What's the difference between preventing the system from sleeping altogether and idle sleeping when caffeinate is running for the duration of a utility's execution?

  • OneCricketeer
    OneCricketeer over 6 years
    That is just running the caffinate command
  • Ludovic Kuty
    Ludovic Kuty over 3 years
    I guess that the "Turn display off after" setting in the Energy Saver System Preferences would be in fact better be called "Idle System Sleep" because it looks like it breaks my OpenVPN connections (ping-restart timeout). caffeinate -i prevents it. So it doesn't just turn the display off because it has a major side-effect. macOS version is 10.15.6. I never had the problem with 10.14.