Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Background Processes

Run commands in the background while continuing to use the shell.

Basic Usage

Add & at the end of a command:

#> long_running_command &
[1] 12345
#>

The shell prints:

  • Job number in brackets [1]
  • Process ID 12345
  • Returns to prompt immediately

Job Control

List Jobs

#> jobs
[1]  Running    sleep 100 &
[2]  Running    make &

Bring to Foreground

#> fg %1
# Job 1 now in foreground

Send to Background

#> bg %1
# Continues job 1 in background

Stopping Jobs

Kill by Job Number

#> kill %1

Kill by PID

#> kill 12345

Job Completion

When a background job completes:

[1]  Done       sleep 10 &

Common Patterns

Run Long Command

make &
# Continue working while make runs

Multiple Background Jobs

command1 &
command2 &
command3 &
# All three run simultaneously

Redirect Output

long_command > output.log 2>&1 &
# Run in background, save output to file

Discard Output

noisy_command &> /dev/null &

Background Process Behavior

stdin

Background processes have stdin disconnected from terminal:

cat &
# Will receive EOF immediately

Signals

Background processes:

  • Ignore SIGINT (Ctrl+C)
  • Receive SIGHUP when shell exits (unless using nohup)

Exit Warning

When exiting with running jobs:

#> exit
hash: There are running jobs.
#> exit
# Second exit forces quit

Practical Examples

Build While Working

make clean && make &
# Edit files while building

Download in Background

curl -O http://example.com/large-file.zip &

Run Server

python -m http.server 8000 &

Watch Logs

tail -f /var/log/syslog &

Tips

Check if Job Running

jobs
# or
ps aux | grep command

Wait for Completion

command &
pid=$!
# Do other work...
wait $pid
echo "Command finished"

Nohup for Persistent Jobs

For jobs that should survive logout:

nohup long_command &
# Output goes to nohup.out

Limitations

  • Job control is basic compared to bash
  • No disown command
  • No sophisticated job status tracking