The best tasks to give a coding agent are the long ones: a migration, a test suite to fix, a refactor across fifty files. They're also the ones a closed terminal or a sleeping laptop kills halfway through.
Four things keep a long task alive: something that outlives the terminal, a computer that stays awake, a way to resume, and a way to answer the agent when you're not at your desk.
1. Outlive the terminal: tmux
Claude Code, Codex and Gemini CLI are interactive programs. They need a terminal, so nohup and & don't help. tmux does: it holds the terminal for you.
tmux new -s refactor # a named session claude # start the agent inside it # Ctrl-b d # detach; the agent keeps working tmux attach -t refactor # come back later, from any terminal Close the terminal window, log out of SSH, quit your terminal app: the agent keeps going. One tmux window per agent keeps several organised (Ctrl-b c for a new window, Ctrl-b , to name it).
2. Stay awake
A sleeping computer stops every agent, and a turn in flight can fail when its network connection drops. Keep the machine awake while agents work:
- macOS:
caffeinate -iin another tmux window, orcaffeinate -i claudeto keep it awake exactly as long as the agent runs. - Linux:
systemd-inhibit --what=idle:sleep claude. - Windows: the power settings, or PowerToys Awake.
Closing a laptop's lid usually sleeps it regardless; leave it open, or plug in an external display.
3. Resume when something does stop
Restarts happen. The conversation isn't lost: claude -c continues the most recent conversation in that folder, and claude --resume lets you pick one. The work in progress is in the files and the git history, so the agent can pick up where it was.
4. Answer it from your phone
A long task usually stops at least once to ask something. If you're away, it waits until you're back. The do-it-yourself fix:
- Put your computer and phone on the same private network with Tailscale, so the computer is reachable from anywhere without opening ports.
- Install an SSH client on your phone (Termius, Blink Shell or similar).
- SSH in and run
tmux attach -t refactor. You're in the agent's session, on a phone keyboard.
It works, and it's free. It's also a terminal UI on a five-inch screen, which is fine for "yes, run the tests" and painful for anything longer.
The app version
I build Moontors, a free desktop app for coding agents, and it does this without tmux.
Every session runs in a background service rather than in the window, so closing the window, or quitting the app, doesn't stop the agents; reopen it and every session is there with what it did while you were away. It doesn't keep your computer awake, so step 2 still applies.
For step 4 there are Android and iPhone apps. You pair the phone with your computer once, by QR code, and it talks to your computer directly, on your network or through Tailscale, with no server in between. The phone shows the board, each session's transcript, and the questions and permission requests waiting for you, and you can reply from it.
It runs Claude Code, Codex, Gemini CLI and Grok Build on macOS, Windows and Linux. The guide is How to keep coding agents running when the window closes.
Summary
| Problem | Do-it-yourself fix |
|---|---|
| Terminal closes | tmux |
| Laptop sleeps | caffeinate -i / systemd-inhibit / power settings |
| Something restarts | claude -c or claude --resume |
| Agent asks while you're out | Tailscale + SSH + tmux attach from your phone |
AI did the writing, I did the reading, and we both took credit.
