How to keep Claude Code running after you close the terminal

Close the window and your agent dies mid-task. Here is why, and five ways to keep a coding agent working while you walk away — from tmux to a session daemon.

3 min read

A closed laptop at night with a terminal session still glowing and working inside it

You give Claude Code a big task, it starts working through it, and you close the terminal window to get it out of the way. The task is gone. Or the laptop sleeps on the train and the session is dead when you open it.

Coding agents are long-running processes, and terminals were never designed for those. Here is why the session dies, and five ways to make sure it does not.

Why closing the window kills the agent

A terminal window owns a pseudo-terminal. When you close the window, the system sends a hangup signal (SIGHUP) to everything running in it, and by default processes exit when they receive it. Claude Code is just another process in that window, so it goes too — along with anything it was in the middle of.

Every fix below works the same way: put something other than the window in charge of the session.

Option 1: tmux (the standard answer)

tmux runs your session inside a server process that keeps going when the window closes. You can detach and reattach at will — even from another machine over SSH.

tmux new -s agent        # start a named session
claude                   # run Claude Code inside it
# press Ctrl-b, then d   # detach — close the window if you like
tmux attach -t agent     # reattach later
tmux ls                  # list running sessions

Best for: most people. Trade-offs: you have to remember to start inside tmux, the keyboard shortcuts take a day to learn, and scrolling and copying work differently from a normal terminal.

Option 2: GNU screen

The older sibling of tmux, with the same idea: screen -S agent to start, Ctrl-a d to detach, screen -r agent to reattach. It is often already installed on servers where tmux is not.

Option 3: headless print mode

If the task does not need you at all, run Claude Code non-interactively with -p (print mode) and detach it from the terminal with nohup:

nohup claude -p "Run the test suite and fix any failing tests" > agent.log 2>&1 &

It keeps running after you close the window, and the output lands in agent.log. Later, claude --continue picks the most recent conversation back up interactively.

Best for: scripted, well-defined jobs. Trade-off: no back-and-forth — if the agent needs a decision, it cannot ask you. Set permissions deliberately before running anything unattended.

Option 4: run it on another machine

Run the agent on a desktop at home, a dev box or a cloud VM, inside tmux, and connect over SSH. Your laptop can sleep, travel or run out of battery, and the session does not care.

Best for: long jobs and heavy builds. Trade-off: another machine to set up, keep in sync and pay for.

Option 5: a workspace whose sessions live in a daemon

The approach we took with NabuCode: sessions do not live in the window at all. A background daemon called nabud owns every terminal. The app is just a view onto it, so:

  • Close the app and the agents keep working. Reopen it and every pane is exactly where it was — scrollback included.
  • Updates and crashes are harmless. The UI is the disposable part; the sessions are not.
  • You can check in from your phone. The companion app attaches to the same sessions, gets the prompts, and lets you answer them.
  • You never think about tmux. No prefix keys, no remembering to start inside a session — every pane is already persistent.

We explain the architecture in your sessions should not live in the window.

Do not forget: stop your Mac from sleeping

Whatever you choose, a sleeping Mac pauses everything on it. To keep a Mac awake while an agent works:

caffeinate -i

That prevents idle sleep until you press Ctrl-C. Run caffeinate -i -t 7200 to keep it awake for two hours. Closing a MacBook's lid still sleeps it unless it is connected to power and an external display.

Which should you use?

Your situationBest option
Occasional long tasks on your laptoptmux
Fire-and-forget scripted jobsHeadless print mode with nohup
Very long jobs, or a laptop you carry aroundAnother machine over SSH
Several agents every dayA daemon-backed workspace like NabuCode

The aim is simple: closing a window should be a decision about the window, not about the work. Pick any of the setups above and you will never lose an agent mid-task again. Running several at once? Read how to run multiple Claude Code agents in parallel.

← All posts