# Your SSH link to the client's server drops. Is the job still running?

> Typing one tmux command before you start a long job means you can reattach to the old session after a dropped connection, so nothing has to run again.

Original: https://fdetimes.net/en/tools/tmux-ssh-drop-keep-jobs-alive/

The official tmux manual puts it plainly: sessions are persistent and survive accidental disconnection, such as when an SSH connection times out. For an FDE running commands on a client's server over a flaky VPN or meeting-room Wi-Fi, that one line matters more than any other feature in the tool.

The risk is that a long job started directly in an SSH window is tied to that connection. A data migration halfway through, or a three-hour backfill, can die just as you are presenting to the client.

The tmux Getting Started guide recommends exactly this: to protect programs running on a remote server from connection drops, run them inside tmux. The advice is simple, but using it properly means understanding how tmux keeps a session alive.

## Why does the session survive when you lose the connection?

The tmux wiki defines it as a terminal multiplexer. It lets you switch between several programs in one terminal and detach from them while they keep running in the background. You can then reattach from a different terminal.

The key is how tmux organizes its processes. It keeps all of its state in a single main process, the tmux server, which runs in the background on the server machine itself. Your SSH window is only a client looking at that server, so a network drop loses the client while the server and the programs inside it carry on.

The server starts automatically with the first tmux command you type, so there is nothing to launch by hand; the only thing to check in advance is whether tmux is installed on the client's machine. By default the server also exits when no programs are left running, and that is the first limit to remember.

## One command for every login

Take a team that needs to run a script moving customer data to a new schema on the client's own server. Straight after SSHing in, before doing anything else, type:

```bash
tmux new-session -A -s migrate
```

The `-A` flag is what makes the command repeatable. According to the manual, if a session named `migrate` already exists, `new-session -A` behaves like `attach-session`, attaching you to the existing session instead of creating a new one. The first time you get a fresh session; every time after that, the same command takes you back to where you left off.

Inside the session, run the script as usual. Say the client's VPN cuts out midway. You SSH back into the machine, type the same line, and the script is still running with its output intact on screen.

One irritating case comes up often: the old connection has not been fully closed, so the session still thinks a client is attached. In that case, use `attach-session` with the `-d` flag and name the session explicitly:

```bash
tmux attach-session -d -t migrate
```

The manual states that `-d` detaches any other clients attached to the session, so the ghost client is removed and you regain full control.

The session name matters because `attach-session` with no target picks the most recently used session that nobody is attached to. The `migrate` session still appears to have a client, so tmux may drop you into a different session.

**Key point:** On a client's machine, never run a long job outside a tmux session.

## Where tmux will not save you

The biggest limit comes from the very fact that all state lives in one server process. A session lives only while something is running inside it: if the script exits with an error and the shell closes too, the server can shut down and take the error output with it.

For the same reason, if the machine itself reboots, the session is gone. Run jobs from an interactive shell inside the session, and write logs to a file.

The tmux FAQ also states that tmux is not a file transfer program. It preserves your terminal but does nothing to push a large dump file across an unreliable link. That needs a different tool.

The last point is security, which matters on a client's shared servers. The FAQ explains that tmux relies on file system permissions to limit who can access its socket.

It follows that if a team shares one system account, anyone who logs into that account can attach to your session and see what you are doing, including variables holding credentials.

## Turn the habit into a runbook

Do not start with key bindings or config files. Learn exactly three things: `new-session -A -s`, `attach-session -d -t`, and the server/client model behind them. From there, fold the limits above into a short runbook template for any long job on client infrastructure:

```bash
# 1. Log in with your own account, not a shared one
ssh <your-own-account>@<client-host>

# 2. Name the session after the ticket
tmux new-session -A -s <ticket>

# 3. Run the job from an interactive shell, logging to a file so there is evidence if the session is lost
./job.sh 2>&1 | tee ~/logs/<ticket>.log

# 4. After a network drop: clear the stale client and attach to the right session
tmux attach-session -d -t <ticket>
```

Step 1 deals with the socket on shared machines, step 2 tells you and your colleagues which session belongs to which piece of work, and step 3 covers the chance that the session disappears along with the server.

When an FDE interviewer asks how you would run a risky migration on a client's machine, walking through these four steps and the reason for each is far more convincing than "knows tmux" on a CV.

## Break it once on purpose

On a staging machine, open a session with `tmux new-session -A -s drill` and run a loop that stands in for a backfill, such as `for i in $(seq 1 600); do echo $i; sleep 1; done | tee ~/drill.log`. When the counter reaches a few dozen, turn off Wi-Fi on your laptop and wait until the SSH window reports the connection lost.

Turn Wi-Fi back on, SSH in and type `tmux attach-session -d -t drill`. If the counter is still climbing and the log file has no gap, you have first-hand proof that the job survived the drop.

Run this drill once on staging, and on the day the client's VPN cuts out in the middle of a real migration, all you need to do is retype the same command.

**Try this week:**

- Next time you SSH into a staging machine, run tmux new-session -A -s work before starting anything. Then turn off Wi-Fi midway to confirm for yourself that the session survives.
- Add a mandatory line to your deployment runbook: every long migration or backfill must run in a tmux session named after its ticket.
- Ask the client's admin whether you can use your own account on shared machines, then check the permissions on the tmux socket.

## Sources

- [Home · tmux/tmux Wiki (GitHub)](https://github.com/tmux/tmux/wiki)

- [tmux(1) manual page, OpenBSD](https://man.openbsd.org/tmux)

- [Getting Started · tmux/tmux Wiki (GitHub)](https://github.com/tmux/tmux/wiki/Getting-Started)

- [FAQ · tmux/tmux Wiki (GitHub)](https://github.com/tmux/tmux/wiki/FAQ)
