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

07
2014-07
  • user12345678

    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?

  • Answers
  • suspectus

    -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

  • davidholcer

    I do not know much about the python script you are referring to, but if you want your your computer not to go into sleep mode, there is a handy-dandy application called Caffeine that does that for you.


  • Related Question

    How to prevent OSX from putting a computer to sleep when a VM is running in VMWare Fusion
  • Jeff Leonard

    I have discovered the hard way that bad things happen to my VMs if my macbook pro goes to sleep while a VM is running.

    Is there a way to prevent my computer from being put to sleep when VMWare Fusion is running a VM but allow it to be put to sleep otherwise?


  • Related Answers
  • redacted

    To paraphrase the iPhone ads, there's an app for that: Caffeine

    It is a menubar app. Click prevents computer from sleeping, click again and the computer can sleep like normal.

  • 8088

    Leave it plugged into the power adapter, and set this in System Preferences -> Energy Saver.

    Power Adapter Settings

    When its on battery, set it to sleep after however long you want.

    This might not be the exact solution you're looking for, but I don't know of any software that will automatically handle this without some kind of intervention. Apps like Caffeine or InsomniaX require doing something and can be easily forgotten.