How To Prevent System Sleep while a Terminal Command is Running: caffeinate
As part of my work, I frequently work remotely, relying on a cellular data via a personal hot spot on my iPhone.
Even with a cell phone booster, internet speed can be quite limited (think 10K/sec at times!), and sometimes flaky too—remote areas don’t exactly have great cell phone coverage, if at allm and an hours-long driving round trip is a huge time-waster.
Here’s a nasty little behavior of macOS: it doesn’t much care what you are doing in something like Terminal—computation, network access, disk access, etc are all ignored. So you take a short break, only to find that macOS has put the machine to sleep and effectively aborted the job.
Example
I use a source code management system for my web servers called git, and I frequently do a git push to sync my local development system to the server:
git push
But if macOS sleeps the system, the process goes to sleep, and when it wakes up the network connection is aborted. All that cellular data gone to waste, and job not done.
The solution is to prevent system sleep until the push is done using 'caffeinate':
caffeinate git push
Since I do this a lot, I like to make a shortcut (alias) for it:
alias push="caffeinate git push"
Example 2
Suppose you want to verify a large volume 'Photos' using IntegrityChecker. You want the system to sleep only after the verify has finished.
caffeinate icj verify Photos