---
title: "How do I keep a command running after I close the terminal?"
description: "Closing a terminal sends SIGHUP and stops the jobs it started. Use nohup, disown or tmux to keep a long-running command alive, and know where its output goes."
url: https://www.danny.engineering/answer/bash/how-do-i-keep-a-command-running-after-i-close-the-terminal
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Bash"]
---

# How do I keep a command running after I close the terminal?

## The short answer


When a terminal closes, your shell and its jobs get a hangup signal, `SIGHUP`, which stops them by default. Start the command with `nohup` so it ignores the signal, send its output to a file, and put it in the background with `&`. If you want to come back and watch it later, run it inside `tmux` instead.


```bash
nohup npm run build > build.log 2>&1 &
echo $! > build.pid
```


## Why it happens


Every terminal session has a controlling process, usually your shell. When the terminal window closes or an SSH connection drops, the system sends `SIGHUP` to that shell. Bash then passes `SIGHUP` on to every job it started, and the default action for that signal is to terminate.


Adding `&` on its own is not enough. It only runs the command in the background so you get your prompt back. The command is still one of the shell's jobs, so it still receives the hangup, and its output is still tied to a terminal that is about to disappear.


## How to fix it


### Use nohup for a one-off command


`nohup` runs a command with `SIGHUP` ignored. If you do not redirect output yourself, it writes to a file called `nohup.out`, so it is clearer to choose the log file:


```bash
nohup ./import-data.sh > import.log 2>&1 &
echo $! > import.pid

# check on it
tail -f import.log

# stop it later
kill "$(cat import.pid)"
```


The special variable `$!` holds the process ID of the last background command, so save it straight away if you will need to stop the process.


### Disown a job you already started


If the command is already running in the foreground, you do not need to restart it. Pause it, resume it in the background, then remove it from the shell's job list:


```bash
# press Ctrl+Z to suspend the running command, then:
bg
disown
```


In Bash, `disown -h %1` keeps the job in the list for `jobs` but stops the shell sending it `SIGHUP`. Zsh supports `disown` too, and also lets you start a command already disowned with `&!`. Its output still goes to the terminal, so this works best for commands that log to a file.


### Use tmux when you want to come back


`tmux` runs a terminal session that keeps going on its own. You can detach, close the window or log out of an SSH session, and reattach later with everything still on screen:


```bash
tmux new -s build
# run your command, then press Ctrl+B followed by D to detach

tmux ls
tmux attach -t build
```


This is the most useful option on remote servers, where a dropped connection would otherwise kill a long migration or deployment.


### Use a service manager for long-lived processes


If the process should run permanently and restart after a crash or a reboot, it is a service, not a background job. Use your system's service manager, such as systemd on Linux or launchd on macOS, or the process manager your platform provides.


## Common pitfalls

- Getting the redirect order wrong. `> file 2>&1` sends both output streams to the file; `2>&1 > file` sends errors to the terminal.
- Leaving `nohup.out` to grow without limit in whatever directory you happened to be in.
- Backgrounding a command that waits for input. It stops when it tries to read from the terminal. Give it no input with `< /dev/null`.
- Using `nohup` with a pipeline or a chain of commands. It only applies to the first command, so wrap the rest in a shell: `nohup sh -c 'npm ci && npm run build' > build.log 2>&1 &`.
- Forgetting the process is still running. Check with `ps` or `pgrep -fl build` before starting another copy.

## Related reading

- [GNU Bash manual: Job Control](https://www.gnu.org/software/bash/manual/html_node/Job-Control.html)
- [GNU Coreutils: nohup invocation](https://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html)
