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

6

1

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?

user12345678

Posted 2014-06-17T22:59:21.013

Reputation: 63

Answers

8

-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

suspectus

Posted 2014-06-17T22:59:21.013

Reputation: 3 957

0

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.

davidholcer

Posted 2014-06-17T22:59:21.013

Reputation: 86

That is just running the caffinate command – cricket_007 – 2017-08-30T01:02:54.180

0

Save the following lines in the ~/Library/LaunchAgents folder as a plist file such as caffeinate.plist . It will auto start when you log in. There are options to prevent screen, harddrive from sleeping. Just man caffeinate.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple Computer/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
<key>Label</key>
<string>org.caffeinate.plist</string>
<key>Program</key>
<string>/usr/bin/caffeinate</string>
<key>ProgramArguments</key>
<array>
    <string>/usr/bin/caffeinate</string>
    <string>-i</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>  

YH Wu

Posted 2014-06-17T22:59:21.013

Reputation: 101