跳到正文
DEV Community· Roi Dayan·· 3 小時前AI 評分65

如何讓 Claude Code 在關閉終端機後繼續運行,並用手機回覆

Keep Claude Code running after you close the terminal (and answer it from your phone)

AI 導讀

文章整理了讓 Claude Code 等編碼智能體在關閉終端機後繼續運行、並可用手機回覆的做法,涵蓋保留工作階段、避免電腦睡眠、恢復工作及遠端連線。自行設定的做法包括用 tmux 保留終端工作階段、用 claude -c 或 claude --resume 恢復對話,以及透過 Tailscale 和手機 SSH 客戶端接入。

正文

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 -i in another tmux window, or caffeinate -i claude to 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:

  1. Put your computer and phone on the same private network with Tailscale, so the computer is reachable from anywhere without opening ports.
  2. Install an SSH client on your phone (Termius, Blink Shell or similar).
  3. 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.

來源:DEV Community · dev.to