How do I keep a command running after I close SSH?
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.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
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.
$ (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 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:
$ 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:
tmux new -s buildstarts a session namedbuildand shows it.- Run the command in it.
- Press
Ctrl-b, release, then pressdto detach. You are back in the normal shell and the program runs on. - Later,
tmux attach -t buildshows 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:
$ 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.
$ 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:
$ 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:
$ 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:
$ 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.
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.
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 manpages.ubuntu.com
- nohup(1) on Ubuntu 24.04 manpages.ubuntu.com
- nohup(1) at man7.org, the hangup signal and nohup.out man7.org
- bash(1) on Ubuntu 24.04, signals, disown and the exit status of a command killed by a signal manpages.ubuntu.com
- tmux(1) on Ubuntu 24.04 manpages.ubuntu.com
- screen(1) on Ubuntu 24.04 manpages.ubuntu.com
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.