Keep your Mac awake when using Claude Code
It’s been a while since I last posted here, but it’s as good a time as any to pick this back up. Today, I want to share a handy tip for those of you using Claude Code.
Like most people in tech lately, I’ve been using and experimenting with LLMs and AI Agents. Cursor, Copilot, Cline, Roo Code… I’ve tried many things, but for the last few months, there’s been one clear winner: Claude Code.
I first tried Claude Code shortly after it was released in February, but I didn’t pay too much attention to it after I burned through $10 worth of tokens in 15 minutes. It did work pretty well, but at that cost, it just wasn’t justifiable for my use. I came back a couple of months ago when Anthropic introduced Claude Max (and later Pro) plans that allow for flat fee usage.
I’ve been using it often since then. Although I’m not much of a vibecoder, it does help me with the blank page syndrome I often struggle with when starting a new project or a bigger feature in an existing one. Sometimes I even fully refactor Claude’s output after using it, but I still find it worth prompting first.
Anyway, one of the main annoyances I’ve been dealing with is that when Claude works for a long time, my Mac may go to sleep and pause the agent. I often leave the agent running while I go make a coffee or get a snack, and it’s very annoying to return to the computer only to find that it hasn’t been doing anything in the meantime.
When Claude Code was updated with hooks support, I thought I could finally address this by just running caffeinate
when it starts working and stopping it when it finishes. However, there was no hook for when it started working—until recently, that is. The UserPromptSubmit
hook was added in version 1.0.54.
I’ve built these two scripts to be run as hooks that will prevent the Mac from sleeping only while Claude is running.
prevent_sleep.sh
#!/bin/bash
# Prevent Mac from sleeping while Claude Code is running (for upto 1 hour)
# Kill any previously running caffeinate process started by this script
if [ -f /tmp/claude_caffeinate.pid ]; then
old_pid=$(cat /tmp/claude_caffeinate.pid)
if ps -p "$old_pid" > /dev/null 2>&1; then
# Ensure the process is caffeinate (full command line check)
if ps -p "$old_pid" -o args= | grep -q '^caffeinate'; then
kill "$old_pid" 2>/dev/null
fi
fi
rm -f /tmp/claude_caffeinate.pid
fi
nohup caffeinate -i -t 3600 > /dev/null 2>&1 &
echo $! > /tmp/claude_caffeinate.pid
allow_sleep.sh
#!/bin/bash
# Re-enable Mac sleep by killing the caffeinate process and cleanup the PID file
if [ -f /tmp/claude_caffeinate.pid ]; then
kill $(cat /tmp/claude_caffeinate.pid) 2>/dev/null
rm /tmp/claude_caffeinate.pid
fi
And then added the hooks to .claude/settings.json
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "$HOME/.claude/hooks/allow-sleep.sh"
}
]
}
],
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "$HOME/.claude/hooks/prevent-sleep.sh"
}
]
}
]
}
I’ve found this to work very well, hope you find it useful as well.