Home of Bobby Yan

A site all about me

I am Bobby Yan, an enthusiastic maker of things. Call me a programmer, web developer, designer or a teacher. Currently a sophomore at UC Berkeley studying Electrical Engineering and Computer Science.

Check out my bio!

How to prevent your Mac from sleeping, a Different way

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.

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."
        echo "Sorry, $1 could not be found."
    echo "Enter the name of the process: "
    echo "Example:"
    echo " prevent_sleep wget"

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.

Recent Posts

Easiest Way to Set Up a Shadowsocks Server

VPSYou could use any service. Here I’m using Vultr’s Los Angeles server since that’s what works for me. Another really good hosting provider is Linode, but j...…

Older Posts

An Introduction to Markdown

Getting to know MarkdownMarkdown is a lightweight markup language with many advantages, and it’s growing in popularity among writers, editors, etc. Now, don’...…