By default, your Mac computers go to sleep automatically after some period of inactivity which is rather appropriate given how short most battery life is. However, this becomes annoying when you’re downloading much larger files. We’re talking about those that take hours.

If you aren’t careful, what might happen is this: you start the download, go for a walk, hit the gym, or take a power nap before coming back to the office to see if your download’s finished, only to realize your Mac’s sleeping too.

Mac Energy Saver Settings

Normally, on days that you’re careful, what you do is go to “Settings” > “Energy Saver” and drag the “Computer sleep” slider all the way to “Never”. This solution is simple enough, but you risk forgetting to set the energy saver back to where it was after you’re done with all the downloads. So, I set out to look for another solution.

Do you know that when media players, like iTunes, are running, as long as something is playing, your Mac won’t go to sleep. Let’s have a look at what has happened.

Before and after iTunes starts playing pmset is an in-built command line utility for power management in macOS. Now it is clear to us that before iTunes started playing, sleep was working fine, but sleep was prevented by iTunes, coreaudiod once we’ve begun playing a song.

Now that we’ve found the key, some Google searches would show us this command called caffeinate that basically keep your Mac from sleeping. Here’s the man page of caffeinate.

Man page of caffeinate

With everything we need, there are just two more things to do:

  1. Get the PID of the process we want to monitor.
  2. Use “caffeinate” to prevent idle sleep when this process/program is running.

Since we’ve only got two things at hand, a script should do. I named the script prevent_sleep and put it under /usr/local/bin.

#!/bin/sh
if [[ -n "$1" ]]; then
    pid=$(ps rax | awk '{print $1, $5}' | grep -i "$1" | cut -d ' ' -f 1)
    if [[ -n $pid ]]; then
        caffeinate -s -w "$pid" &
        echo "System sleep prevented by $1."
    else
        echo "Sorry, $1 could not be found."
    fi
else
    echo "Enter the name of the process: "
    echo "Example:"
    echo " prevent_sleep wget"
fi

This script first checks for processes with the given name, and looks for its PID if the process exists, before passing the PID to caffeinate. As long as the monitored process is running, caffeinate keeps running, and your computer won’t go to sleep.

Ok, it’s time for some coffee.

Hey! Thanks for reading :P I just want to let you know that if you enjoyed this, you might also like this post on Sorting.

ABOUT THE AUTHOR RSS Feed Icon

I'm Bobby Yan, a programmer, music lover and web developer. I am currently working on IB Notes, a website with useful and concise study guides and notes for IB students. I have also built the Spanish Vocabulary Builder iOS app to help Spanish learners.

If you enjoyed this article, you should follow me on Twitter or sign up to get an email whenever I write something new:

Share this article with your friends: