60

I'm running some Python machine learning programs through Terminal that take hours at a time to finish. If I leave my computer unattended it hibernates - the screen goes off and I have to type in my password again.

When this happens, does my script get cancelled, paused, or continue the same? I would hate to think that my script could be 99% finished, and then gets aborted because my computer went to sleep.

3
  • To prevent your Mac from falling asleep Caffeine is a useful little tool.
    – Emil
    Commented Mar 31, 2017 at 13:00
  • 34
    Also, your Mac has a built in tool called caffeinate. If you precede the terminal command with caffeinate, e.g. caffeinate python ..., it will not sleep for the length of this particular command.
    – 0942v8653
    Commented Mar 31, 2017 at 13:44
  • 2
    @0942v8653, I have just experimented with this command, and it seems like you need to supply -s flag to caffeinate, like this: caffeinate -s python .... But I also clicked the Sleep button in the system menu, to check how this utility behaves; it probably affects the results. Commented Feb 26, 2021 at 16:10

3 Answers 3

46

All processes get paused when the system goes to sleep, independent of whether the process is part of macOS, an application or some code you wrote on your own. After the system wakes up again, all processes will continue to run.

PS: Processes which were waiting for network or disk data when going to sleep may run into timeouts though. But that can happen anyway so most processes should be able to handle that anyway.

0
43

Yes, terminal processes like a python script will stop execution when the machine goes into idle sleep.

To prevent your machine from sleeping without changing the settings you can just use the terminal tool caffeinate. Once called it will prevent the machine from sleeping until you cancel the program using ctrl+c.

You can pass arguments like the process id to caffeinate using the -w flag. You can find out a process id using the top command in the terminal, e.g. caffeinate -w 36388 would prevent the machine from going into sleep until that process is finished.

See the attached link for more information.

https://www.howtogeek.com/245683/how-to-prevent-your-mac-from-sleeping-with-no-additional-software/#:~:text=To%20open%20the%20Terminal%2C%20you,you%20leave%20the%20Terminal%20running.

2
  • While you offer a solution, you don't offer an actual answer to the question. Which is what happens to a Python script when my Mac sleeps? I would recommend editing your answer. Commented Jun 5, 2020 at 13:06
  • 1
    Also answers on AD should be more than just a link. Referencing the source is highly appreciate but please include the relevant parts directly in your answer so it stays useful even if the link breaks.
    – nohillside
    Commented Jun 5, 2020 at 13:20
18

Note that there is a difference between computer sleep and display sleep (both controlled in the Energy Saver pane of System Preferences). When the display is sleeping but the computer is not, programs on your computer continue to run normally. When your Mac enters computer sleep, programs are paused, and will resume when the computer is awakened (but some programs, especially those that rely on network communications, can't resume seamlessly).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .