Home

How to exit a terminal window without killing its running processes

We’ve all been there. You’ve SSH-ed into a server and started running some long running task.

But you didn’t expected it would start taking that long… And now you can’t close your laptop and stop the task midway.

The bellow steps allow you to close the terminal window, without killing the process.

The easiest solution

  1. Ctrl + Z to suspend the process
  2. bg to resume the process in the background
  3. disown -ah to remove all jobs from the shell and make them ignore SIGHUP
  4. exit to close the terminal

Demo

For experimenting purposes, try the following:

terminal 1
bash -c "sleep 10" & disown -ah && exit

On a new terminal, you can see sleep is still in the process list. After 10 seconds it closes as expected:

terminal 2
ps aux | grep sleep

You can learn more about it here and here.