DEV Community

Little Pan
Little Pan

Posted on Originally published at ponymux.com

How to pick up Claude Code and Codex sessions after a restart

On Friday you had four agents going: one on the API, one on the docs, a Codex session running tests, and one you'd parked halfway through a refactor. Over the weekend macOS installed an update and restarted. On Monday the tabs are gone, and you don't remember which session was in which folder.

The conversations aren't lost. What stopped is the processes:

  • What's saved: every message up to the moment the agent stopped. Claude Code and Codex write the transcript as they go.
  • What isn't: a reply that was still being written, and the running process itself.
  • Bring one back: in the project folder, run claude --continue or codex resume --last.
  • Bring several back: list your most recent sessions with their folders, pick the ones you were using, and resume each. There's a command below that prints them for you.

What happens to a running agent when I close the terminal or restart?

Closing a terminal window, quitting the terminal app, logging out, and restarting all end the processes inside, including Claude Code and Codex. Whatever the agent had already written to its transcript stays on disk. A turn that was halfway through is cut off.

Sleep is different. When the Mac sleeps, processes pause and pick up where they were when it wakes. A request that was in flight to the model can fail while the network is down. If the agent reports an error after wake, tell it to continue.

Some terminal apps reopen your windows after a restart, with the same folders and scrollback. That looks like the session is back, but it's a fresh shell. The agent isn't running in it.

How do I resume the last session in a project?

Open a terminal in the project folder and run:

# Claude Code
claude --continue

# Codex
codex resume --last
Enter fullscreen mode Exit fullscreen mode

Both pick the most recent session from the current directory. If you ran more than one session in that folder, use the picker instead: claude --resume or codex resume. The folder matters. Run the same command from your home directory and you get a different session, or none.

How do I bring several sessions back at once?

When several agents were running in different folders, you need each session's folder and ID. The transcripts don't record whether a session was still running, but the ones you were using before the restart are usually the most recently written. These commands print one ready-to-run line per recent session, most recently active first:

# Claude Code: the 10 most recent sessions
ls -t ~/.claude/projects/*/*.jsonl | head -10 | while read -r f; do
  jq -r 'select(.cwd) | "cd \(.cwd | @sh) && claude --resume \(.sessionId)"' "$f" | head -1
done
Enter fullscreen mode Exit fullscreen mode
# Codex: the 10 most recent sessions
ls -t ~/.codex/sessions/*/*/*/*.jsonl | head -10 | while read -r f; do
  jq -r 'select(.type == "session_meta") | "cd \(.payload.cwd | @sh) && codex resume \(.payload.id)"' "$f" | head -1
done
Enter fullscreen mode Exit fullscreen mode

Each line looks like cd '/Users/you/code/api' && claude --resume 8e539d93-…. Pick the ones you were working on, open a tab per session, and paste its line. A session that sat idle for a long time can fall below newer ones, so raise head -10 if one is missing. Skip lines whose folder no longer exists, such as a worktree you've since removed.

Both commands read each tool's transcript files directly. That format is internal and can change between releases, so treat these as a convenience, not an interface. If you turned on Codex's experimental compression, older sessions are .jsonl.zst files and won't show up in the list. Both commands need jq, which ships with macOS 15 and later. The pickers work too: press Ctrl+A in claude --resume to see every project, or run codex resume --all. The list above is faster when you want several sessions back at once.

How do I keep an agent running while I'm away?

Two different problems, two different tools.

The Mac going to sleep. An idle Mac sleeps, and the agent pauses with it. caffeinate keeps the system awake for as long as the command inside it runs:

caffeinate -i claude
Enter fullscreen mode Exit fullscreen mode

-i prevents idle sleep. It doesn't stop sleep when you close the lid or choose Sleep yourself.

The terminal closing. A terminal multiplexer such as tmux keeps processes running after the window closes:

tmux new -s api      # start a session, then run claude or codex inside it
tmux attach -t api   # later, from any terminal window
Enter fullscreen mode Exit fullscreen mode

tmux survives closing the terminal and quitting the terminal app. It doesn't survive a restart or a logout. After a restart you're back to resuming sessions.

Can I skip looking up folders and session IDs?

In PonyMux, each Terminal is a saved record: its name, folder, List, and the sessions that ran in it. Quitting PonyMux or restarting the Mac ends the processes, but the Terminals stay where you left them. They come back sleeping, marked with a zzz badge.

Select a sleeping Terminal that had an agent conversation, and PonyMux shows the last exchange from that session, so you can see where it stopped. With automatic resume on, a short countdown starts. When it runs out, PonyMux resumes that exact session in the Terminal's folder. You can resume right away, pause the countdown, or choose Shell Only for a plain shell instead.

A sleeping Terminal after a restart. The last exchange is on screen, and Resume counts down before it reopens the session.
A sleeping Terminal after a restart. The last exchange is on screen, and Resume counts down before it reopens the session.

That replaces the list-and-paste step above: each Terminal already knows its folder and session, so bringing four agents back is four clicks. Automatic resume can be turned off in Settings → General. If the transcript is gone, for example after Claude Code's 30-day cleanup, the Terminal says so and offers a shell instead.

For the idle-sleep problem, the coffee-cup Keep Awake button at the bottom of the sidebar prevents idle sleep and App Nap while it's on. Like caffeinate -i, it doesn't override closing the lid.

The Keep Awake button in the PonyMux sidebar footer.
The Keep Awake button in the PonyMux sidebar footer.

For how sleeping and waking work, see Terminal Lifecycle. To find an older session by what was said in it, see Claude Code conversation history.


Originally published on ponymux.com, where the demos are interactive.

Top comments (0)