# How do I keep a command running after I close SSH?

Stop a command from dying when SSH disconnects. See what SIGHUP does, then use nohup, disown, tmux or screen. The commands were run on a server and checked.

Source: https://valakes.com/learn/keep-commands-running/

Last updated: 2026-11-27

Start it inside tmux (tmux new -s name, then Ctrl-b and d to leave). If tmux is not there, put nohup in front of the command and an ampersand after it. When a terminal closes, the programs started in it get a hangup signal and stop, and both tools get around that.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 25 November 2026.

Run the long job in `tmux`: `tmux new -s build`, start the command, then press `Ctrl-b` and then `d`. The job keeps running after you close the window or lose the connection, and `tmux attach -t build` brings it back. If tmux is not installed, `nohup command > job.log 2>&1 &` does the same for a program you do not need to talk to.

## What happens when the session ends

The shell on the server runs on a terminal. When that terminal goes away, the system sends the signal SIGHUP, "hangup detected on controlling terminal or death of controlling process", to the programs that depend on it. The default action of SIGHUP is to end the program. A shell that gets it passes it on to the programs it started.

You can send the signal by hand to see the effect. The status 129 is what bash reports for a command killed by signal 1: the manual says 128 plus the number of the signal.

```console
$ (sleep 310 & kill -HUP $!; wait $!; echo "exit status: $?") 2>/dev/null
exit status: 129
```

The same thing happens at the end of an ssh session. That is why [a broken connection](https://valakes.com/learn/ssh-keep-alive/) kills a build in the middle. Keep-alive settings keep the connection open longer. They cannot save a job when the connection really ends.

## nohup: start the program deaf to the signal

`nohup` runs a command with SIGHUP ignored. If the output would go to a terminal, it goes to `nohup.out` instead, so the program does not write to a terminal that is gone. Add `&` to get your prompt back. Here the signal is sent to a `nohup` job, and the job is still there afterwards:

```console
$ nohup sleep 312 > /dev/null 2>&1 & sleep 1; kill -HUP $!; sleep 1; ps -C sleep -o args= | grep -v infinity
sleep 312
```

## disown: protect a job you already started

You started a long command and forgot `nohup`. In bash, press `Ctrl-z` to stop it, type `bg` to let it run in the background, then `disown -h`. The bash manual says that `disown` removes a job from the shell's table. With `-h` the job stays in the table but is marked so that the shell does not send it SIGHUP. Either way the shell no longer passes the hangup on.

There is no run of this here, because it needs an interactive shell with job control. The program also keeps your terminal as its output, so redirect the output before you start it when you can.

## tmux: a session that outlives the connection

tmux keeps your programs under its own server process, away from your terminal. The manual calls a session persistent: it survives an accidental disconnection, such as an ssh timeout, and an intentional detach. By hand:

1. `tmux new -s build` starts a session named `build` and shows it.
2. Run the command in it.
3. Press `Ctrl-b`, release, then press `d` to detach. You are back in the normal shell and the program runs on.
4. Later, `tmux attach -t build` shows the screen as you left it.

A script cannot press keys, so it starts the session already detached with `-d`. The run below also gives the session a name and a command, and waits a second before the ssh session ends:

```console
$ ssh -tt app 'tmux new -d -s build "sleep 316"; sleep 1; echo started'
started
Connection to app closed.
```

## screen: the older tool with the same idea

`screen` does the same job and is often already installed. `screen -dmS job command` starts the command in a new session called `job`. The manual says `-d -m` starts screen in detached mode: it creates the session and does not attach to it. `screen -ls` lists the sessions and `screen -r job` reattaches. Use whichever your servers have, and learn one of them well.

```console
$ ssh -tt app 'screen -dmS job sleep 317; sleep 1; echo started'
started
Connection to app closed.
```

## See which jobs survive a closed session

Each `ssh -tt` below opens a session with a terminal, as an interactive login does, starts a program in the background, and lets the session end after a second. The first has no protection, the second uses `nohup`:

```console
$ ssh -tt app 'sleep 314 & sleep 1; echo started'
started
Connection to app closed.
$ ssh -tt app 'nohup sleep 313 > /dev/null 2>&1 & sleep 1; echo started'
started
Connection to app closed.
```

Now a new session lists the programs named `sleep 3...`, which include the ones the tmux and screen runs started:

```console
$ ssh app 'ps -C sleep -o args= | grep "^sleep 3" | sort'
sleep 313
sleep 316
sleep 317
```

`sleep 314` is gone. It was hung up with its session. The other three are running: 313 because of `nohup`, 316 and 317 because tmux and screen own them. To list the sessions of the two tools, ask each one:

```console
$ ssh app "tmux list-sessions -F '#{session_name}: #{session_windows} window'; screen -ls | grep -c Detached"
build: 1 window
1
```

## Mistakes people make

You start a job in a plain session and trust that it survives. As the listing shows, it does not, and a job that dies with the session leaves half of its work behind.

You use `nohup` without `&`. The command stays in the foreground and holds your prompt, and Ctrl-C still ends it. Add `&` to get the prompt back.

You forget the output. `nohup` writes to `nohup.out` in the folder where you ran it, and that file grows for as long as the job prints. Send the output to a named file and follow it with `tail -f`, as in [reading server logs](https://valakes.com/learn/read-server-logs/).

You type `exit` in tmux instead of detaching. That ends the shell, and with the last window the session. Detach with `Ctrl-b` and `d`.

You run the same long job on many servers by hand. Start each one in a detached session from a script; the loop is in [running a command on many servers](https://valakes.com/learn/run-command-on-many-servers/).

## Questions

### Should I use nohup or tmux?

Use nohup for a job you start and forget, such as a backup or a build, whose output goes to a file. Use tmux when you want to come back and see the screen, answer a prompt or type into the program.

### Where does the output of nohup go?

If standard output is a terminal, nohup appends it to nohup.out in the current directory, or in your home directory if that fails. To choose the file yourself, redirect the output, as in nohup command > file.

### Does closing the terminal window kill a tmux session?

No. The tmux manual says a session survives an accidental disconnection, such as an ssh timeout, and a detach. The session keeps running on the server, and tmux attach brings it back.

## References

- [signal(7) on Ubuntu 24.04, SIGHUP](https://manpages.ubuntu.com/manpages/noble/man7/signal.7.html)
- [nohup(1) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man1/nohup.1.html)
- [nohup(1) at man7.org, the hangup signal and nohup.out](https://man7.org/linux/man-pages/man1/nohup.1.html)
- [bash(1) on Ubuntu 24.04, signals, disown and the exit status of a command killed by a signal](https://manpages.ubuntu.com/manpages/noble/man1/bash.1.html)
- [tmux(1) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man1/tmux.1.html)
- [screen(1) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man1/screen.1.html)

## Related articles

- [How do I run one command on many servers over SSH?](https://valakes.com/learn/run-command-on-many-servers/): Put the host names in a file and run ssh -n -o BatchMode=yes -o ConnectTimeout=5 host command for each one, in a loop for a few servers or with xargs -P for many. Keep each host's output and exit status, so one host that fails never hides behind the others.
- [How do I stop SSH from dropping with "Broken pipe"?](https://valakes.com/learn/ssh-keep-alive/): Add ServerAliveInterval 30 to the Host * block of ~/.ssh/config. ssh then sends a small message every 30 seconds, so a router that forgets idle connections sees traffic, and ssh notices when the server stops answering.
- [How does SSH port forwarding work? Examples of -L, -R and -D](https://valakes.com/learn/ssh-port-forwarding/): ssh -L 9000:127.0.0.1:8080 app opens port 9000 on your machine and sends every connection to port 8080 on the server app. -R does the reverse, and -D turns the connection into a SOCKS proxy.
