Job Control & Process Management

fg/bg, jobs, disown, nohup, Ctrl+C/Z signals, process substitution, and pipeline control.

Overview

Overview Bash job control allows managing multiple processes from a single terminal session. Understanding it is essential for running background tasks, keeping processes alive after logout, and debugging pipeline behavior.

Terminal signals: Ctrl+C sends SIGINT (signal 2) to the foreground process group — typically terminates the process. Ctrl+Z sends SIGTSTP (signal 20) — suspends the process (pauses it, doesn't terminate). Ctrl+\ sends SIGQUIT — terminates with core dump.

Job control commands: After Ctrl+Z suspends a process, jobs -l lists all jobs with their PIDs and states. fg %1 brings job 1 to foreground. bg %1 resumes job 1 in background (as if it were started with &). & at end of command starts directly in background. Job specs: %1 is first job, %% or %+ is most recent job, %- is previous job.

disown vs nohup: disown removes a job from the shell's job table — the process continues running but the shell won't send SIGHUP when the terminal closes. However, the process's stdin/stdout still point to the terminal. nohup makes a process immune to SIGHUP before starting — output goes to nohup.out by default. For long-running processes, use nohup command > output.log 2>&1 & and then optionally disown.

Process substitution <(command): Creates a named pipe (FIFO) and substitutes its path. Allows commands that expect files to receive command output: diff <(sort file1.txt) <(sort file2.txt). The command runs in a subshell.

pipefail: By default, a pipeline's exit code is the last command's exit code. set -o pipefail makes the pipeline fail if any command fails. Critical for reliable scripts.

Key concepts

SIGINT vs SIGTSTP
Ctrl+C sends SIGINT (terminate). Ctrl+Z sends SIGTSTP (suspend/pause). Suspended jobs can be resumed with fg or bg. SIGSTOP cannot be caught; SIGTSTP can.
disown vs nohup
disown: removes job from shell table after it starts — SIGHUP not sent on shell exit, but stdout still points to terminal. nohup: before starting — immune to SIGHUP, stdout redirected to nohup.out.
Process substitution <()
(command) pipes output into command. Avoids temp files.
pipefail option
set -o pipefail makes pipeline exit code the rightmost non-zero exit code. Without it: cmd1 | cmd2 exit code is only cmd2's. Critical for catching errors in pipelines.

When to use it

  • Running long operations in background while continuing work
  • Keeping processes alive after SSH session disconnect
  • Debugging pipeline failures where intermediate commands fail silently
  • Process substitution to avoid temp files

Interview questions

  • What is the difference between disown and nohup? When would you use each?
  • Explain set -o pipefail. Why is it important for reliable shell scripts?

Sign up free to read the full topic

More Linux topics

Start free on Cariara · Coding problems · System design studio