Hivra
Close the terminal and your shell takes the agent down with it.

Why your AI agent dies when you close the terminal (and every fix that works)

Claude Code, Codex, OpenClaw, Hermes: every terminal AI agent dies when you close the terminal and stalls when the laptop sleeps. The fixes run from a 90 second tmux setup up to systemd, and past those there's an always-on computer at $9.99/month.

Short answer

Your agent is a child process of your shell. Close the terminal or drop SSH and it gets a SIGHUP, and a sleeping laptop suspends it. tmux, screen, nohup or systemd detach it from your session, which handles the hangup. Sleep is a separate problem: keep the machine awake, or move the agent to an always-on machine.

Why the process dies in the first place

Terminal AI agents are ordinary child processes of your shell. That one fact explains almost every dead session, and it shows up three ways.

The one people blame on the agent most is closing the terminal. When a terminal window closes, the kernel sends SIGHUP (hangup) to every process in that session, and the agent exits mid-task. That's how Unix has behaved for 50 years, so it isn't a bug in the agent.

A dropped SSH connection does the same thing on a remote server. The connection tears down the remote shell, which sends the same SIGHUP to the agent. Flaky wifi, a VPN hiccup or your ISP renegotiating is enough.

Sleep is the third, and it works differently. Close the lid and the OS suspends every process on the machine. Nothing is killed, but nothing runs either. A 6 hour overnight task makes zero progress, and any open network connections (including the agent's API calls) usually die during the suspend anyway.

Which one you've got decides the fix. SIGHUP problems (closed terminal, dropped SSH) go away once you detach the process from your session with tmux, screen, nohup or systemd. Sleep doesn't. No multiplexer can make a suspended CPU execute instructions, so for sleep you either keep the machine awake or move the agent to one that stays awake.

Want to know whether your current setup would survive? Run the agent survival check. It goes through the same failure modes and tells you which one will get you.

tmux and screen in 90 seconds

tmux is a terminal multiplexer. It runs your shell session inside a server process on the machine, so the session survives when your terminal window or SSH connection goes away. It's the standard fix for SIGHUP:

bash
# Start a named session and launch your agent inside it tmux new -s agent claude # or codex, or any CLI agent # Detach without killing anything: press Ctrl+b, then d # Reattach later, from any terminal or SSH connection tmux attach -t agent # List running sessions tmux ls

Close the window, lose the SSH connection, reconnect from your phone. The agent never noticed. You reattach and the full scrollback is there. The tmux cheat sheet lists the keys and commands, and builds the start commands for each agent.

screen is the older tool that does the same job:

bash
screen -S agent # start a named session # Detach: Ctrl+a, then d screen -r agent # reattach

Pick tmux if you're choosing today. It has panes, better scripting and active development. screen's one advantage is that it ships preinstalled on more minimal distros.

Both share one limit: tmux on your laptop does not survive sleep. It protects the session from disconnects. It can't stop the machine from suspending, so tmux only earns its keep on a machine that stays awake.

nohup and systemd for real persistence

If you don't need to interact with the agent, you don't need a live session at all. Most CLI agents have a headless or print mode that takes a prompt, works, and exits. nohup makes that run immune to SIGHUP:

bash
nohup claude -p "run the test suite and fix failures" > run.log 2>&1 & # Watch progress tail -f run.log

nohup detaches the process from the terminal, so logging out or closing the window does nothing to it. The tradeoff is that you can't steer. You can't reattach, and you can't answer a question the agent asks. It's fire and forget, which suits tasks that really are fire and forget.

For an agent that should always be running (a Discord bot, a monitoring agent, a long-lived Hermes style assistant), the correct tool on Linux is a systemd service, because systemd also restarts the process when it crashes:

bash
# /etc/systemd/system/my-agent.service [Unit] Description=My AI agent After=network-online.target [Service] ExecStart=/usr/local/bin/my-agent Restart=always RestartSec=5 User=agent [Install] WantedBy=multi-user.target
bash
sudo systemctl daemon-reload sudo systemctl enable --now my-agent journalctl -u my-agent -f # logs

Restart=always is what tmux doesn't give you. tmux keeps a session open, and systemd keeps a process alive. If the agent segfaults at 3am, systemd starts it again 5 seconds later.

Mac-specific fixes: caffeinate, pmset, Amphetamine

On a Mac, the usual killer is sleep, and macOS gives you real tools for it:

bash
# Keep the Mac awake for as long as this command runs caffeinate -i claude # Or keep it awake while an already-running process lives (by PID) caffeinate -i -w 12345 # Or keep it awake for a fixed window, e.g. 8 hours caffeinate -i -t 28800

caffeinate -i prevents idle sleep while the wrapped command runs. The keep-awake command builder writes the right flags for your setup. It's the cleanest overnight fix on a Mac that stays plugged in and open.

If that isn't enough, pmset changes power settings system wide. sudo pmset -a disablesleep 1 blocks sleep entirely, including lid close. Remember to set it back with sudo pmset -a disablesleep 0, or your battery will remind you. Amphetamine (free, Mac App Store) is the GUI version: one click keeps the Mac awake for a set time, with an option to let the display sleep while processes keep running.

The catch that gets everyone: closing the lid still sleeps most MacBooks unless the machine is plugged in AND connected to an external display, or you've forced it with pmset disablesleep. caffeinate alone does not override lid close. If your overnight plan is a closed MacBook in a backpack, the plan is the problem.

The overnight-run checklist

Before you start a long run and go to bed, spend 2 minutes on this list:

  • Detach the session. Agent running inside tmux (or via nohup/systemd), not bare in a terminal window.
  • Kill sleep for the window. caffeinate -i on Mac, disable suspend in power settings on Linux and Windows, or run on a machine that stays awake.
  • Plug it in. Battery power plus a long agent run is a race you lose.
  • Log to a file. Redirect output (> run.log 2>&1) or rely on tmux scrollback so you can see what happened at 4am.
  • Cap the blast radius. Set spending limits with your model provider, and give the agent a scoped task with a clear done condition, not an open loop.
  • Check for prompts. Many agents pause on permission questions. Pre-approve what you're comfortable with (for example a permissive mode inside a sandbox or VM), or the agent will sit waiting for input for 7 hours.
  • Test the survival, not the task. Start the run, close the terminal, reattach. If it survived that, it will survive the night's disconnects.

The failure modes none of this fixes

tmux, nohup and caffeinate fix the shell problem. The machine is a separate problem, and plenty of things on it can still end your run.

If the agent process itself crashes, tmux just shows you a dead pane in the morning. Only a supervisor (systemd with Restart=always, or a managed platform) restarts it. OS updates on macOS and Windows can force a reboot overnight, and anything not registered as a boot service is gone. A power blip or your router's 2am firmware update kills the run on any home machine.

Then there's the simple fact that your laptop has another job. Sooner or later it goes in a bag. Every fix above works around one thing: a personal machine isn't an always-on server.

Keeping one machine awake for one night is easy. Keeping an agent alive for weeks, through crashes, reboots, updates and your own travel, is server operations, and the fixes above only delay that work.

When a $10 always-on computer beats all of it

Every fix in this article does the same thing: it moves the agent's lifetime off your terminal, then off your laptop. Follow that far enough and you end up with a machine whose only job is to run the agent.

You can build that yourself with a $5-10/month VPS plus tmux plus systemd plus your own patching and security. It's a perfectly good route, and we wrote it up in detail for Claude Code and Codex. The full menu of places an agent can live, from home hardware to serverless, is in the AI agent hosting guide.

Or you can rent that machine. Hivra provisions a private VM per agent and runs the agent on it: Claude Code (the official CLI, signed in with your own Anthropic account), Codex (the official CLI, your own ChatGPT login), and Hermes, and hosts the dashboard for Aeon. You get a browser view of the computer (chat, terminal, files), so checking on it at 4am doesn't mean SSH from your phone.

One rule from this article still applies there, though. On a paid Hivra plan the computer stays on and keeps your files, sessions and login. A Claude Code or Codex run you start inside tmux in the computer's Terminal tab keeps going after you close the laptop. On Claude Code, work you send through Telegram, connected in the agent's Telegram tab, runs on the computer, not in your browser. Hermes, OpenClaw and Agent Zero run on the computer itself, and Aeon's scheduled tasks run on your own GitHub Actions while the computer hosts its dashboard. None of them needs an open browser.

Plan math: the $9.99/month plan is 2 vCPU and 4 GB of RAM, and the $19.99/month plan is 4 vCPU and 8 GB of RAM. Paid plans are not paused for inactivity, and they come with a 7-day money-back guarantee on card payments. On your own Anthropic or OpenAI login, usage bills through that provider at its rates with no Hivra markup. Hivra is independent and is not affiliated with Anthropic or OpenAI. Details on the pricing page.

If you enjoy running servers, run the server. If you just want the agent alive tomorrow morning, and every morning after, the $9.99 computer (2 vCPU and 4 GB of RAM) is this whole article without the upkeep.

Common questions

Why does my AI agent stop when I close the terminal?

The agent is a child process of your shell. Closing the terminal sends SIGHUP to the whole session and the agent exits with it. Run the agent inside tmux or screen, or start it with nohup, and it survives the terminal closing.

Why does my AI agent keep stopping when my laptop sleeps?

Sleep suspends every process on the machine, so the agent makes no progress and its network connections usually die during the suspend. tmux doesn't help here. Keep the machine awake (caffeinate on Mac, power settings on Linux/Windows) or run the agent on an always-on machine like a VPS or a managed computer.

Does tmux keep an AI agent running after I disconnect from SSH?

Yes. tmux runs the session inside a server process on the remote machine, so a dropped SSH connection changes nothing. Reattach with tmux attach -t <name> and the agent is exactly where you left it. It only fails if the remote machine itself sleeps, reboots, or loses power.

How do I run an AI agent overnight on a Mac?

Start the agent inside tmux, then keep the Mac awake with caffeinate -i (or Amphetamine), leave it plugged in, and leave the lid open unless you have disabled lid sleep with pmset. Log output to a file so you can review what happened. Note that closing the lid still sleeps most MacBooks even with caffeinate running.

What is the difference between nohup and tmux for AI agents?

nohup detaches a one-shot process from the terminal so it survives logout, but you can't reattach or interact with it afterward. tmux keeps a full interactive session alive that you can detach from and reattach to. Use nohup for fire-and-forget headless runs, tmux when you might need to steer.

What keeps an AI agent running after a crash or a reboot?

None of the session tools do. tmux and nohup only protect against disconnects. For crash recovery you need a supervisor like a systemd service with Restart=always, and for reboots the service must be enabled at boot. On a managed platform like Hivra, the agent's VM and its services are set up for you, so you don't write that unit yourself.

Give your agent a computer that stays on.

From $9.99/mo. Bring your own AI key. 7-day money-back guarantee on card payments.

Start Now