AI coding on the go: keeping an unplugged Mac awake with the lid closed

I’m a member of the Innovate Newport co-working space and sometimes Claude Code is still earnestly coding when it’s time for me to leave at the end of the day. Leaving means I need to unplug my Mac laptop and close the lid. The Mac’s built-in caffeinate command doesn’t help here: it will keep the computer awake with the lid closed only when on AC power.

Looking around online, I found a variety of approaches to solving this and the one that I liked best was a no-nonsense script in an old StackExchange post from 2017. I made my own version with a few refinements, which I’m happy to share. I kept the original author’s name for it: noz (for “no 💤”). You pass it the number of minutes to stay awake. Here’s an example:

$ noz 20
Password:

Preventing sleep for 20 minutes; press <enter> to continue...
Restoring previous battery sleep setting: 5

It changes the laptop’s sleep settings so it requires sudo. Then at the end it restores the original settings.

A couple things that should have been obvious to me, but I still managed to forget more than once 🤦‍♂️:

  • If you’re on the move you’ll want to switch to mobile wifi first, like tethering to your phone.
  • Don’t put the laptop in a sealed carry case/sleeve. Since it’s not sleeping, it may got hot.

I made just a few tweaks to the original: mine takes input in minutes instead of seconds, restores the original sleep settings, and runs sudo before displaying its messages instead of after.

Note if you look online, someone made a Homebrew version, also called noz, which I’m not linking to, as it looks remarkably similar to the StackExchange script but doesn’t credit it. It’s about 5 times as much code, mostly for visuals like a countdown, etc. but no additional functionality.

Leave a Reply