Tutorial

Keep a Claude Code Session Alive After Closing the Terminal

Akshay Sarode
Direct answer

Don't run Claude Code from a terminal that owns the process. Spawn it from a daemon that lives independently of any shell. When you close the terminal — or close the laptop lid — the agent keeps running. When you come back, you re-attach.

Anthropic's Remote Control documentation calls this out in plain text: "Your terminal must remain open. Your machine must stay online." For an interactive coding session that's fine. For an agent that takes hours — or one you want to start before getting on a flight — it's a hard constraint.

Why "open terminal" is the constraint

Most Claude Code sessions inherit the lifetime of the parent shell. When you Cmd-Q the terminal, the shell sends SIGHUP to its children. Claude Code dies. With Remote Control specifically, the terminal also has to stay attached to claude.ai/code's WebSocket session.

The fix: daemon owns the process

Instead of running Claude Code as a child of your shell, run it as a child of a daemon. The daemon is a long-lived process registered with launchd / systemd / Windows service manager. It survives shell exits, terminal exits, and (on macOS) lid closes when the laptop is plugged in.

This is what celistrad does. The daemon spawns the agent in a fresh PTY, holds the read end of stdin/stdout, and detaches the agent from any terminal. You can attach a browser tab to watch the PTY — the daemon fans out the same byte stream to every attached client. Close the tab, agent keeps running. Open a new tab, the last 64KB replays so you have context.

The flow

# Spawn (from any terminal, or the dashboard, or your phone)
celistrad ctl spawn 'claude --resume my-session'

# Close the terminal you spawned from. Quit your terminal app entirely.
# Close the laptop lid (with power plugged in).
# An hour later:
celistrad ctl list
# ag-7f3a · running · 47m elapsed · pid 8821

celistrad ctl attach ag-7f3a
# (re-attaches to the live PTY)

What happens at sleep

macOS distinguishes lid-closed-with-power from lid-closed-on-battery. With power plugged in, the daemon stays running. On battery, the OS suspends — the daemon and its children all freeze. When you wake, they unfreeze and continue. PTY clients (browser tab, ssh, dashboard) re-establish their WebSocket and replay.

The case it doesn't survive: full reboot. For that, mark the agent autoRestart: true on spawn — the daemon respawns with the same command, env, and cwd on next launch. Up to 5 retries in any 60-second window before backing off.

What happens when the network drops

If you're attached from a phone over LTE and the LTE drops, the agent doesn't care — it's running locally. The phone reconnects, the dashboard fetches the last 64KB of replay, you see what happened while you were offline.

What about nohup and tmux?

Both work. nohup claude in a terminal then close the terminal — Claude Code keeps running. tmux new-session 'claude' — same. The daemon approach gives you three things nohup and tmux don't:

If you don't need any of those, nohup is fine.

The simplest possible recipe

If you don't want to install anything new, this is the smallest fix:

# Spawn detached from your shell
nohup claude --resume my-session > ~/claude-$(date +%s).log 2>&1 &
disown

# Close the terminal. Process keeps running.
# To re-attach (read-only):
tail -f ~/claude-*.log

# To kill:
pkill -f 'claude --resume my-session'

You lose: interactive input after disowning, multi-client view, sandbox, history. You keep: simplicity.

The Celistra recipe

celistrad ctl spawn --auto-restart \
  --workspace ~/code/myproject \
  'claude --resume my-session'

You get all of the above plus the daemon-managed lifecycle. Spawn from anywhere (terminal, dashboard, phone). Re-attach from anywhere. Survive lid close, network drop, terminal exit, laptop reboot.

FAQ

Does this work on Linux?

Yes. systemd-managed celistrad survives shell exits and tty hangups. The PTY fanout is the same.

Does this work on Windows?

Yes. The Windows service version of celistrad runs under Local System and survives user logoff. ConPTY handles the PTY layer.

Does claude.ai/code Remote Control work over the daemon's PTY?

Not directly — Remote Control is Anthropic-side and doesn't know about your local daemon. Use the daemon for persistence and the dashboard or your phone for re-attaching.

Can I see what an agent did while I was offline?

Yes. The daemon keeps a 64KB scrollback ring per agent and a 30-day SQLite history of every spawn (with command, exit code, runtime). The dashboard renders both.