Do Terminal processes stop if Mac sleeps?

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.


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.


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.


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).