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.
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.
_10nohup npm run build > build.log 2>&1 &_10echo $! > 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:
_10nohup ./import-data.sh > import.log 2>&1 &_10echo $! > import.pid_10_10# check on it_10tail -f import.log_10_10# stop it later_10kill "$(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:
_10# press Ctrl+Z to suspend the running command, then:_10bg_10disown
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:
_10tmux new -s build_10# run your command, then press Ctrl+B followed by D to detach_10_10tmux ls_10tmux 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>&1sends both output streams to the file;2>&1 > filesends errors to the terminal. - Leaving
nohup.outto 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
nohupwith 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
psorpgrep -fl buildbefore starting another copy.
Related reading
#Was this resource helpful?