TExcelTAD
Home › Linux & Bash › Processes & Jobs

Processes & Jobs

When you start a command from Bash, it becomes a child process of the shell. Normally the shell waits for it to finish (a foreground job), but with & it keeps running in the background while you continue working. Bash's job control lets you list, pause, resume and stop these jobs, and wait lets scripts run tasks in parallel.

Background jobs: & and jobs

bash · try it yourself
sleep 30 &
sleep 40 &
jobs
kill %1
wait %1
echo "job 1 ended with status $?"
jobs
kill %2
[1]-  Running                    sleep 30 &
[2]+  Running                    sleep 40 &
job 1 ended with status 143
[2]+  Running                    sleep 40 &
Notation Means
%1, %2 Job number 1, 2
%+ or %% The current job (marked +)
%- The previous job (marked -)
%sleep The job whose command starts with "sleep"

Job numbers are local to your shell; PIDs are system-wide. kill %1 and kill <PID> both work.

Foreground, background, suspend (interactive)

In an interactive terminal you can move jobs between foreground and background:

text
$ ./long-build.sh            # running in the foreground…
^Z                           # Ctrl+Z suspends it
[1]+  Stopped                 ./long-build.sh
$ bg                         # continue it in the background
[1]+ ./long-build.sh &
$ jobs
[1]+  Running                 ./long-build.sh &
$ fg %1                      # bring it back to the foreground
./long-build.sh
Keys / command Effect
Ctrl+Z Suspend the foreground job (SIGTSTP)
bg [%n] Resume a stopped job in the background
fg [%n] Bring a job to the foreground
Ctrl+C Interrupt the foreground job (SIGINT)

Job control is switched off in scripts by default; these keys are for your terminal.

Running tasks in parallel with wait

wait pauses until background jobs finish. That's the simplest way to run independent tasks at the same time in a script:

bash · try it yourself
compress() {
  local name=$1
  seq 1 20000 > "$name.txt"
  gzip "$name.txt"
  echo "$name done" > "$name.status"
}
for part in alpha beta gamma; do
  compress "$part" &
done
wait
cat alpha.status beta.status gamma.status
ls *.gz
alpha done
beta done
gamma done
alpha.txt.gz
beta.txt.gz
gamma.txt.gz

The three jobs ran concurrently, so they could finish in any order. Each wrote its result to its own file, and the script printed them in a fixed order after wait: a reliable pattern for parallel work. Printing directly from parallel jobs would mix their output unpredictably.

Collecting exit statuses

wait PID returns that job's exit status, so you can check every task:

bash · try it yourself
check() { sleep 0.2; [ "$1" != "broken" ]; }
declare -A pids
for site in shop blog broken; do
  check "$site" &
  pids[$site]=$!
done
failed=0
for site in shop blog broken; do
  if wait "${pids[$site]}"; then echo "$site: ok"; else echo "$site: FAILED"; failed=1; fi
done
echo "overall status: $failed"
shop: ok
blog: ok
broken: FAILED
overall status: 1

$! is the PID of the most recent background job; the script stores it in an array and never prints it.

Surviving logout: nohup, disown, setsid

When you close a terminal, its jobs receive SIGHUP and usually die. To keep something running:

Tool How
nohup cmd & Ignore SIGHUP; output goes to nohup.out
disown %1 Remove a job from the shell's job table so it won't get SIGHUP
setsid cmd Start in a new session, fully detached
tmux / screen Keep whole terminal sessions alive and reattach later
systemd service The proper way for anything long-running (cron & systemd lesson)
bash · try it yourself
nohup bash -c 'echo "started"; echo "finished"' > task.log 2>&1 &
wait
cat task.log
started
finished

Process relationships

Every process has a parent (PPID). Your terminal runs a shell, the shell runs your commands, and on the whole system everything descends from PID 1 (systemd or init). Tools to explore:

text
$ pstree -p ada
bash(2311)─┬─sleep(2402)
           └─vim(2398)
$ ps -o pid,ppid,stat,cmd --forest

Zombie processes (Z in ps) have finished but haven't been "reaped" by their parent with wait; a few are harmless, thousands mean a buggy parent program.

Test yourself

What does & at the end of a command do?

  1. Runs it as root
  2. Runs it in the background
  3. Runs it twice
  4. Links two commands

In a script, how do you wait for all background jobs to finish?

  1. sleep
  2. wait
  3. fg
  4. jobs -w

A job was suspended with Ctrl+Z. Which command resumes it in the background?

  1. fg
  2. bg
  3. kill -9
  4. resume