Background processes: &, nohup, setsid, disown, jobs, wait
Running in the background, bash job control, and how to make a process survive closing the terminal
A command with & at the end runs in the background: the shell doesn't wait for it to finish and returns the prompt right away. Such processes are jobs of the current shell: you can see them in jobs, bring them back to the foreground and wait for them with wait. But when the terminal closes, the shell sends SIGHUP to its jobs and they exit. To make a process survive leaving SSH, you detach it from the terminal: nohup, disown or setsid.
Syntax
command & # run in the background
jobs [-l|-p|-r|-s] # list jobs
fg [%N] / bg [%N] # to the foreground / continue in the background
wait [-n] [PID|%N…] # wait for completion
disown [-h] [-a] [%N] # detach a job from the shell
nohup command [arguments…] [&]
setsid [-f] [-w] command [arguments…]
Job control (built into bash)
| Command / keys | What it does |
|---|---|
command & | run in the background; prints [1] 4242 — job number and PID |
$! | PID of the last background process — save it to a variable |
Ctrl+Z | suspend the current command (signal SIGTSTP); it becomes Stopped |
bg [%N] | continue a stopped job in the background |
fg [%N] | bring a job back to the foreground |
jobs | list: [1]- Running sleep 100 &; + — current, - — previous |
jobs -l / -p | with PIDs / PIDs only |
jobs -r / -s | running only / stopped only |
kill %N | send a signal to a job by number |
Job references: %1 — by number, %+ or %% — current, %- — previous, %sleep — by the start of the command, %?str — by substring.
wait
| Form | What it does |
|---|---|
wait | wait for all background processes; exit code 0 |
wait PID / wait %N | wait for a specific one; the exit code is that process's exit code |
wait -n | wait for the first one to finish, whichever it is; its exit code |
wait -n -p var | same, and store the PID of the finished one in var (bash 5.1+) |
If there is no such process among the shell's children — exit code 127.
Surviving terminal close
| Method | How it works | Output |
|---|---|---|
nohup cmd & | runs the command ignoring SIGHUP | if stdout is a terminal, writes to ./nohup.out (or ~/nohup.out), mode 600 |
cmd & disown | removes the job from the shell's table — it won't send it SIGHUP | still attached to the terminal — redirect it yourself |
disown -h %N | the job stays in jobs, but it won't get SIGHUP | same |
setsid cmd | runs in a new session with no controlling terminal; PID 1 becomes the parent | same — redirect it yourself |
tmux / screen | the terminal lives on the server, you can reconnect | everything is visible after you log back in |
| systemd unit | the proper way for long-running services | journalctl |
setsid options: -f — always fork a new process (return control immediately), -w — wait for the program and return its exit code, -c — make the current terminal the controlling one.
Examples
# run in the background and remember the PID
sleep 300 &
pid=$!
jobs -l
kill "$pid"
# continue a stopped command in the background
# (you pressed Ctrl+Z during a long cp)
bg
jobs
# several tasks in parallel and wait for all of them, checking codes
for host in web1 web2 web3; do
ping -c1 -W1 "$host" >/dev/null 2>&1 &
done
wait
# wait for a specific one and get its exit code
(sleep 1; exit 3) &
wait $!
echo "code: $?" # 3
# survives leaving SSH; output to its own log, stdin closed
nohup ./long-task.sh > task.log 2>&1 < /dev/null &
# already started without nohup — detach after the fact
./long-task.sh > task.log 2>&1 &
disown
# fully detach from the terminal and session
setsid -f ./daemon.sh > /tmp/daemon.log 2>&1 < /dev/null
Pitfalls
- A plain
cmd &dies when SSH closes. An interactive shell that receivesSIGHUPforwards it to all its jobs.nohup,disown,disown -handsetsidprotect against that. nohupdoesn't redirect output if it's not a terminal already.nohup.outappears only when stdout is a terminal. Anddisownandsetsidnever redirect: a process writing to a closed terminal gets a write error and may crash. Always add> file 2>&1 < /dev/null.nohupdoesn't do&for you.nohup cmdwithout&holds the terminal until it finishes.waitwith no arguments returns 0 even if jobs failed. If you need each exit code, save$!and callwait "$pid"one by one.waitonly sees its own children. A process started from another shell or detached withsetsid -fcan't be waited for this way: "is not a child of this shell", exit code127. For other processes, loop onkill -0 PID.jobsand%Nwork only in the shell that started the job. In a new SSH sessionjobsis empty — find the process withpgrep/ps.- A background process that reads from the terminal gets stopped (
Stopped (tty input)). Close stdin:< /dev/null. $!is the PID of the last element of a pipelinea | b &, not the first.- For real services
nohupis a hack: no restart on crash and no proper logs. Use systemd or supervisor.
See also
kill · ps · timeout · tmux · redirection · supervisorctl · exit-codes