# Keep a script running after closing SSH

> Closing an SSH session sends a hangup signal to everything started in it. On an EnvHaven workspace the terminal is already a persistent tmux session; on a server you manage, tmux, nohup or a service keeps the script going.

_Updated November 23, 2026_

When an SSH connection closes, the shell sends SIGHUP to the processes it started, and most of them exit. On an EnvHaven workspace the terminal already guards against that. On a server you manage, three tools do it, and they suit different jobs.

## On an EnvHaven workspace

An SSH login to a workspace lands in its terminal, which is one tmux session shared with the browser and the Haven CLI. Add your public key, or import it from GitHub, under Remote Access in the workspace's EnvHaven sidebar, then log in, start the script in a window of that session and close the connection whenever you like:

```bash
ssh abc@ssh-<workspace>.envhaven.app
# you are now inside the workspace's tmux session; Ctrl+B, then C opens a new window
python bot.py
```

The script keeps running while you are away. The machine never sleeps, so nothing else has to stay on, and your next SSH login or the workspace's browser tab shows the same window with its output.

If the script is a web app or a webhook, bind it to `0.0.0.0:3000` and it also gets a public HTTPS address at the workspace's preview hostname. A process does not come back by itself after a workspace restart or update, so start it again after one.

## On a server you manage yourself

### tmux: for anything you want to come back to

```bash
tmux new -s bot
python bot.py
# detach: Ctrl+B, then D
```

The script keeps running inside tmux after you log out. `tmux attach -t bot` brings the terminal back, output and all. This is the right choice for agents and anything interactive.

### nohup: for a fire-and-forget command

```bash
nohup python bot.py > bot.log 2>&1 &
```

The process ignores the hangup and writes its output to `bot.log`. You cannot reattach to it, only read its log and stop it by process id.

### A service: for something that must survive a reboot

On a server with systemd, a unit file restarts the script when it crashes and when the machine restarts:

```ini
[Service]
ExecStart=/usr/bin/python3 /home/me/bot.py
Restart=always
```

This is the most robust option and the most setup.

### The machine still has to stay on

All three keep a process alive after you disconnect; none of them helps if the machine itself stops. A laptop that sleeps, or a hosted environment with an idle timeout, stops the script along with everything else.

## Keep reading

- [tmux](https://envhaven.com/glossary/tmux)
- [Run a Python script 24/7](https://envhaven.com/guides/run-a-python-script-24-7)
- [Host a Discord bot 24/7](https://envhaven.com/guides/host-a-discord-bot-24-7)
- [Background coding agents](https://envhaven.com/guides/background-coding-agents)

Source: https://envhaven.com/guides/keep-a-script-running-after-closing-ssh
