Running Claude Code Overnight: tmux, Permission Modes, and a Cloud VM That Does the Setup
Claude Code Overnight Long-Task Playbook: tmux, Permissions, Acceptance, and Why I Switched to ApexOne
If you've used Claude Code for a while, you've probably run into this awkward situation: you give it a big task, like refactoring a module or filling out a full test suite, it works away for half an hour, and you're ready to head home. Close the laptop? Wi-Fi drops, SSH disconnects, and the task dies with it, leaving the project in a half-finished state.
This post organizes how I've been getting Claude Code to work overnight: from the basics of tmux, to making it truly unattended, preventing it from causing trouble, and how to review the results the next day. Finally, I talk about how to choose the machine. You can try it out tonight.
1. First, figure out the problem
Claude Code is a process that runs in a terminal. To run long tasks, two things need to be true at once:
- The process must not follow the session: if you run locally, it depends on your terminal window; if you SSH to a server, it hangs under that SSH session. When the session drops, the process gets killed with it.
- The machine must stay on: if the laptop sleeps when you close the lid, no matter how resilient the process is, it's useless.
The first thing goes to tmux, the second depends on what machine you use, details in section 6.
2. tmux: keep the process independent of the session
sudo apt install -y tmux # macOS: brew install tmux
tmux new -s work # create a session named work
cd ~/your-project
claude # start Claude Code inside tmux, give it the task
After handing off the task, press Ctrl+B then D to detach from the session. Now you can close the terminal, drop SSH, or shut the laptop—Claude Code keeps running inside tmux. When you come back:
tmux attach -t work # return to the session, all output is there
Pitfalls I've hit:
- Always use
Ctrl+B Dto leave the session, never typeexit.exitcloses the whole session, and Claude goes down with it. - You can check progress without entering the session:
tmux capture-pane -t work -p -S -50prints the last 50 lines of output, good for a quick SSH peek from your phone. - Don't use nohup for interactive mode: Claude Code's interactive UI needs a real terminal. Backgrounding with
nohup claude &often causes problems (it exits when it can't get input). Either use tmux or use the-pmode described below. tmux lsshows sessions;Ctrl+Bthen%splits vertically, so you can watch Claude work on one side while you run tests or look at logs on the other.
3. Make it truly unattended: permission settings
tmux only solves “the process stays alive,” but if you come back at midnight and see a paused prompt asking “Allow execution of npm install?”, you've wasted the night. By default, Claude Code stops and asks you for every risky action, so for overnight tasks you need to arrange this in advance.
1. Choose the right permission mode
When starting, you can specify a permission mode:
claude --permission-mode acceptEdits # editing files no longer asks, running commands still does
claude --permission-mode auto # a classifier checks for you: normal operations pass, dangerous ones are blocked
auto is a decent compromise for overnight work: installing dependencies, running tests, and editing code don't ask you, but dangerous operations like force-pushing or directly piping a downloaded script to bash are blocked.
There's also --dangerously-skip-permissions, which, as the name says, blocks nothing. Only use it on an isolated VM or container; on a machine with all your projects and keys, that's like handing over the whole computer.
2. Write hard rules with settings
No matter which mode I use, I put a few denial rules in the project's .claude/settings.json as a last line of defense:
{
"permissions": {
"allow": [
"Bash(npm test:*)",
"Bash(npm run lint:*)",
"Bash(git add:*)",
"Bash(git commit:*)"
],
"deny": [
"Bash(git push:*)",
"Bash(rm -rf:*)",
"Read(./.env)",
"Read(./.env.*)"
]
}
}
Meaning is straightforward: running tests and committing code are fine; pushing to remote, rm -rf, and reading .env are not. Code stays on a local branch, and I review before deciding to push.
4. Communicate the task clearly: the key to a successful overnight
My experience: whether an overnight task runs well depends 80% on the task description you write in the minutes before bed. During the day you can correct course, but at night it only follows what you wrote.
1. Save a snapshot before starting
git checkout -b night/refactor-auth # create a dedicated branch
git add -A && git commit -m "pre-start snapshot" # if it messes up, you can always come back here
2. Write project rules in CLAUDE.md
The CLAUDE.md at the project root is read by Claude every time, good for “rules it can't guess on its own”:
## Project Rules
- Test command: npm test (all must pass to be done)
- Code style: npm run lint must have no errors
- After each small independent task, git commit once, with a message that clearly describes what changed
- If unsure about a design choice, pick the simpler, more conventional one, and record your reasoning in NOTES.md
- If the same problem is tried 3 times without success, write it to the “pending” section in NOTES.md and move on to the next task
The last two are especially important: don't let it stop to ask you questions: instead, let it make a decision, leave a record, and you review the next day.
3. Write the task as “goal + acceptance criteria”
Don't say just “refactor the auth module,” but:
Refactor src/auth from session to JWT.
Completion criteria:
1. All existing tests pass, and add unit tests for the new token validation
2. Don't change external API paths or return formats
3. Commit after each step
When done, write a summary in NOTES.md: which files changed, and what you're unsure about
With clear completion criteria, it knows when it's done and won't stop halfway thinking it's good enough.
5. Two advanced uses
1. claude -p: fully unattended
If the task is clear enough and doesn't need mid-way interaction, use non-interactive mode:
claude -p "Complete the task per TASK.md, commit after all tests pass" \
--permission-mode auto \
--max-turns 80 \
> night.log 2>&1 &
-p makes it exit after the task, output goes to the log; --max-turns sets a cap to prevent endless loops on a problem. With cron, you can schedule tasks, like running dependency upgrades and tests every morning at 2 am.
2. Notify yourself when it's done
Instead of waking up to check, have it tell you when it finishes. Claude Code's Hooks can run a command when it stops, add to .claude/settings.json:
{
"hooks": {
"Stop": [
{
"hooks": [
{ "type": "command", "command": "curl -s -X POST https://your-notification-url -d 'Claude task done'" }
]
}
]
}
}
Replace the URL with your usual push service (WeCom / Feishu / DingTalk bot webhooks all work), and in the morning your phone shows what time it clocked out.
6. Where does the machine come from: three options
Everything above is about “how to run,” the last question is “where to run.” I've tried three ways, each with trade-offs:
| Own computer | Own VPS | Out-of-the-box cloud dev machine | |
|---|---|---|---|
| Prep work | Nearly zero | Install OS, Node, Claude Code, Docker, and toolchains | Boot in ~2 minutes, everything preinstalled |
| Closing lid / network loss | Not possible, must stay on, prevent sleep | No impact | No impact |
| Security isolation | AI can touch everything on your computer | Isolated from local | Isolated from local, separate VM |
| Suitable for | Occasional overnights | Willing to tinker, need 7×24 services | Want to skip env setup and ops |
Own computer: easiest, but must stay plugged in and powered on. On macOS, use caffeinate -i to prevent sleep, or disable sleep on power in System Settings. Downside: AI sits on the same machine as all your files and keys, so permissions must be tightened.
Own VPS: the classic option. Pick a 2-core 4G+ instance (Claude Code plus project deps and tests will struggle with too little memory), and follow the steps above to set up. If you need 7×24 services, this is the best fit.
Out-of-the-box cloud dev machine: I recently switched to ApexOne (apex1.us), for a simple reason: my own VPS required reinstalling the whole environment every time I changed machines, and most of the time it sat idle anyway. ApexOne essentially prepares that whole VPS environment in advance. A full Ubuntu VM with root, Claude Code, Codex, Node, Python, Docker, git, and tmux all installed, ready in two minutes; if you don't want to configure SSH, the browser-based web terminal works directly. It also has auto-sleep: after idle for a while, it sleeps, and incoming connections wake it up, fitting an episodic usage pattern. No Claude account needed: new users can use the preconfigured Claude Code in the machine to try it out; for long-term use, choose the platform-logged-in Claude Pro (a paid option).
7. Common failure scenes
After many overnights, I've seen all these cases:
| Symptom | Cause | Fix |
|---|---|---|
| Stopped halfway in the morning, says usage limit reached | Pro / Max subscriptions have usage caps based on time windows, long tasks can easily hit it | After quota resets, run claude --continue in the same directory to continue the previous conversation; make tasks smaller, don't jam too much in one night |
| Gets confused in the middle of the night, repeats things already done | Conversation too long, early details get compressed | Have it write progress to NOTES.md (what's done, what's next), use /compact mid-way and continue per NOTES.md |
| Stuck on one problem all night | Waiting for your answer on “should I do this?” | Write in CLAUDE.md: “if unsure, choose the simpler option and record, don't ask” |
| A test keeps failing, goes back and forth | Test itself is flaky, or problem exceeds its capability | Write “try 3 times then record and skip”, you review the next day |
| Changes too big to review | Task too large | One goal per night, require commits after each step, rather run several nights |
8. How to accept the work the next morning
Regardless of where it ran, I follow this order in the morning:
git log --oneline night/refactor-auth # what it committed overnight
npm test # did tests really all pass?
git diff main...night/refactor-auth # review changes file by file
cat NOTES.md # its summary and “unsure” notes
Mentally prepare: overnight output won't be 100% usable, some will need rework or be thrown out. So I have it work on a dedicated branch and commit after each step; merge the usable parts, roll back the unusable. Even so, waking up to a few hours of work done is a good deal.
9. Checklist for tonight
- Install tmux and Claude Code on a machine that can stay on
- Create a new branch,
git commitsnapshot - Write deny rules in
.claude/settings.json(no push, norm -rf, no reading.env) - Write test command, commit-per-step, and “record instead of asking” rules in CLAUDE.md
- Write task as “goal + acceptance criteria”
-
tmux new -s work, start with--permission-mode auto - Configure Stop Hook notification (optional)
-
Ctrl+B Dto detach, go to sleep
If you want to try an out-of-the-box cloud dev machine: ApexOne new users get $2 trial credit, no card required, enough to run a 2-core 4G machine for a full day. SSH public key optional; after setup, use the web terminal directly. Claude Code is preconfigured—type claude to experience it (~3 hours of Sonnet usage included).
Registration: https://apex1.us/?ref=ba1-a User QQ group: 1128167392
If you like my articles, follow my WeChat public account: 【前端技术官】.
Main sharing: Frontend Architecture · AI Programming · Career Insights · Developer Growth
Scan QR with WeChat to follow 👆
Irregular updates, no spam, only genuinely useful content.
Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.
tmux keep-alive plus clear acceptance criteria is indeed key. If the permission whitelist is not tightened, running overnight is risky. Thanks for sharing the pitfalls. I usually browse practical articles on this kind of AI programming tools on ai345 by category, and the updates are quite fast.
❤️