10.2. Job Control#

10.2.1. Common Pitfalls#

1. Forgetting to redirect output from background processes

# Bad: Output floods your terminal
tar -czf backup.tar.gz /var &
# tar output mixes with your prompt

# Better: Redirect to files or /dev/null
tar -czf backup.tar.gz /var > /tmp/backup.log 2>&1 &
tar -czf backup.tar.gz /var &> /dev/null &

2. Not using wait() in scripts with background jobs

# Bad: Script exits before job finishes
process1 &
process2 &
echo "Done!"  # Exits immediately, jobs may not finish

# Better: Wait for jobs
process1 &
process2 &
wait
echo "Done!"  # Now waits for both jobs

3. Closing terminal with important background jobs

# Jobs started with & are killed if terminal closes
# Solution: Use nohup or screen/tmux

nohup long_job &
# or
screen
# then detach with Ctrl+A, D

4. Confusing job numbers with PIDs

# jobs shows job numbers [1], [2], etc.
# kill requires PID
fg %1         # Job number notation (% prefix)
kill %1       # This doesn't work!
kill 2345     # Need actual PID
ps aux | grep command  # Find PID if unsure

10.2.2. Real-World Example: Parallel Backups#

#!/bin/bash

# Run multiple backups in parallel with job control

echo "Starting backups..."

# Start backup jobs in background
tar -czf /backups/home.tar.gz /home &
job1=$!
echo "Backup 1 (PID $job1) started"

tar -czf /backups/var.tar.gz /var &
job2=$!
echo "Backup 2 (PID $job2) started

mysqldump -u root -p database > /backups/db.sql &
job3=$!
echo "Backup 3 (PID $job3) started"

# Wait for all to complete
echo "Waiting for backups to complete..."
wait $job1 $job2 $job3

if [[ $? -eq 0 ]]; then
  echo "All backups completed successfully"
else
  echo "Some backups failed!"
fi

# Show results
ls -lh /backups/

10.2.3. Nohup and Disown#

10.2.3.1. Nohup: Immunity to Hangup#

# Run command immune to terminal hangup
nohup long_command > output.txt 2>&1 &

# Command continues if terminal closes
# Output redirected to nohup.out by default

10.2.3.2. Disown: Remove from Job Control#

# Start process in background
tar -czf backup.tar.gz /var &

# Disown it (remove from job control)
disown %1

# Now closing terminal won't affect process
# Can't control with fg/bg anymore

10.2.3.3. Comparison#

Method

Runs After Logout

Controllable

Signal Handling

&

Only if ssh persistent

Yes

Gets SIGHUP

nohup

Yes

No direct control

Ignores SIGHUP

disown

Depends on shell settings

No

Still gets SIGHUP

Best Practice: Combine all three:

nohup long_command > /tmp/output.txt 2>&1 &
disown %1

10.2.4. Suspending and Resuming#

10.2.4.1. Suspending a Foreground Process#

# Press Ctrl+Z in foreground process
$ sleep 100
^Z
[1]+  Stopped                 sleep 100

# Confirm it's suspended
$ jobs
[1]+  Stopped                 sleep 100

10.2.4.2. Resuming Suspended Processes#

# Resume in foreground (terminal blocked)
fg %1
# or
fg

# Resume in background (terminal free)
bg %1
# or
bg

# Resume by number
fg %2
bg %3

10.2.4.3. Working with Multiple Jobs#

# Multiple suspended jobs
jobs
[1]   Stopped   sleep 100
[2]-  Stopped   nano file.txt
[3]+  Stopped   python script.py

# Bring job 2 to foreground
fg %2

10.2.5. Job Control Basics#

Jobs are shell-managed background processes:

10.2.5.1. Viewing Jobs#

# List all jobs
jobs

# Detailed job info with PIDs
jobs -l

# Show only running jobs
jobs -r

# Show only stopped jobs
jobs -s

10.2.5.2. Job Output Format#

[job_number]  status    command
[1]          Running   tar -czf backup.tar.gz /home &
[2]         Stopped   nano longfile.txt
  • [1], [2]: Job numbers (shell-specific, not PIDs)

  • +: Current job (default for fg/bg)

  • -: Previous job

  • PID: Process ID (OS-level identifier)

10.2.6. Foreground vs. Background#

By default, commands run in the foreground:

# Foreground: Terminal waits for command to finish
ls -lR /var  # Terminal blocked until command completes

Run commands in the background with &:

# Background: Command runs, terminal prompt returns immediately
tar -czf backup.tar.gz /home &
# Output: [1] 2345  (job number and PID)

# You can now run more commands while tar processes
ls -la

# Check if background process is still running
jobs

10.2.6.1. Why Use Background?#

  • Long-running operations (backups, downloads, builds)

  • Multiple tasks in parallel

  • Avoid tying up terminal

  • Run processes during sleep or away from keyboard