Posted on September 15, 2010 at 2:07am EDT. More.

A simple way to keep your Mac awake

This is a meandering post that ends with a shell scripting tip.

Images on this blog had been broken ever since I migrated to NearlyFreeSpeech.NET several months ago. I took some time today to fix them and tie up some other loose ends. I learned that PHP’s safe mode (as implemented by NSFN, anyway) won’t allow a file to be created unless the group of the destination directory and the script being executed match. Once I chgrp’d everything to the “web” group, things I couldn’t get working before, like WP-Super-Cache, started working again.

I use rsync to update my website, and it would be great if it could take care of setting the owner and group appropriately on files it copied to the web server. Unfortunately, rsync will only try to find a group with the same name on both systems, or ignore the group it entirely. There doesn’t seem to be a way to provide your own mapping to rsync, and tell it that the local group “staff” should be mapped to group “web” on the remote system.

Just in case there were some rsync secrets I didn’t know about, I went searching on Super User, and that’s where I browsed my way to this question: Stop my mac from Sleeping whilst a bash script is running and then allow it to sleep as normal when it’s done?

The lone answer to the question merely suggested reading the manual page for pmset. I figured I could offer the world something more helpful than RTFM, so I took a look at it myself. At first I thought I’d have to do a lot of work to figure out the current power settings, save them, disable sleep, do whatever work the Mac needs to stay awake for, then finally restore the saved settings. That would have been especially difficult since pmset’s output isn’t designed for parsing.

However, it turns out pmset has a mode designed just for this situation. It’s barely explained in the manual, but if you call pmset noidle, sleep will be disable for as long as the process is alive. So all you need to do is something like this:

# start background process to disable sleep
pmset noidle &
# save its process ID so we can terminate it later
PMSETPID=$!

...have your fun here...

# kill pmset so the computer can sleep if it wants to kill $PMSETPID

It would be nice if my write up would improve my Super User Reputation, but considering that the question is viewed approximately 0.37 times per day, I’m not holding my breath.