Kahibaro
Discord Login Register

Foreground and background jobs

Understanding Jobs vs Processes (Quick Recap)

A process is a running program.
A job is your shell’s way of tracking processes you started from that shell.

This chapter is about how your shell (typically Bash) lets you run those jobs in the foreground or background and control them interactively.

Key idea:

Running Commands in the Foreground

By default, when you type a command and press Enter, it runs in the foreground:

Example:

sleep 30

The prompt will not return until sleep finishes (30 seconds) or you interrupt it.

Foreground usage is best when:

Starting Jobs in the Background

To start a job directly in the background, add & at the end:

sleep 300 &

What happens:

  [1] 12345

Use background jobs when:

You can run multiple background jobs:

sleep 100 &
sleep 200 &

Each gets its own job number: [1], [2], etc.

Listing Jobs

Use jobs (a shell built-in) to see what your current shell is tracking:

jobs

Typical output:

[1]-  Running                 sleep 300 &
[2]+  Running                 sleep 200 &

Meaning:

jobs only shows jobs started from the current shell session, not every process on the system.

Useful variants:

jobs -l
[1]- 12345 Running                 sleep 300 &
[2]+ 12346 Running                 sleep 200 &

Suspending Foreground Jobs (`Ctrl+Z`)

You can suspend (pause) a running foreground job using:

This sends a SIGTSTP signal, causing the job to stop temporarily.

Example:

  1. Run:
   sleep 300
  1. Press Ctrl+Z:
   ^Z
   [1]+  Stopped                 sleep 300

Now:

Why suspend? To:

Moving Jobs to the Background (`bg`)

After suspending a job, you can resume it in the background with bg.

Basic usage (most common):

bg

Resumes the most recent stopped job.

Example:

sleep 300
^Z
[1]+  Stopped                 sleep 300
bg
[1]+ sleep 300 &

Now sleep 300 runs in the background.

To specify a particular job:

bg %1
bg %2

You can see the effect with:

jobs

bg only works on stopped jobs, not already-running background jobs.

Bringing Jobs to the Foreground (`fg`)

To bring a job to the foreground (so it owns the terminal again), use fg.

Most common:

fg

Brings the current job (+) to the foreground.

Example:

sleep 300 &
# Do something else...
fg

More precise:

fg %1
fg %2

Useful patterns:

While a job is in the foreground:

Job Identifiers: `%` Syntax

Job-control commands (fg, bg, kill in job-control context) often take job IDs prefixed with % instead of PIDs.

Common forms:

Examples:

jobs
[1]-  Running                 long_task &
[2]+  Stopped                 nano notes.txt
fg %+       # bring 'nano notes.txt' to foreground
bg %-       # background the previous job
fg %long    # if 'long_task' is unique

This is convenient when PIDs are long or you remember the command but not the number.

Stopping and Killing Background Jobs

Even though full process management is another chapter, it’s common to control jobs with kill using job IDs.

Kill using a job ID:

kill %1      # send SIGTERM to job [1]
kill -9 %1   # force kill job [1] (SIGKILL)

You can also:

After a job finishes or is killed, you may see a message sometime later:

[1]+  Done                     sleep 300

Or via jobs:

jobs
[1]+  Done                     sleep 300

Once it’s marked Done, the shell forgets it after the next prompt or command.

Background Jobs and Terminal Output/Input

Important behavior differences:

    [1]+  Stopped (tty input)   command

The job stops until you bring it to the foreground with fg.

    long_command > output.log 2>&1 &

This sends both standard output and error to output.log.

Long-Running Jobs and Session Lifetime (`nohup`, `disown`)

By default, when you close the terminal (or log out):

Two common tools to keep jobs alive after logout (detailed behavior is learned later, but you should recognize them):

  1. nohup – run a command immune to hangups:
   nohup long_command > output.log 2>&1 &
  1. disown (Bash, Zsh) – remove jobs from the shell’s control:
   long_command &
   disown %1

The job keeps running even if the shell exits, and it no longer appears in jobs.

These are especially useful when you accidentally start a long task interactively and need to disconnect.

Practical Usage Patterns

Here are common “job control workflows” you’ll likely use often:

1. Temporarily “Parking” a Foreground Task

# You’re in nano editing a config file…
Ctrl+Z               # suspend nano
jobs                 # confirm it's 'Stopped'
bg                   # resume nano in background (dangerous if it needs input)
fg                   # bring nano back when ready

More realistically, editors prefer the foreground; use Ctrl+Z mainly for long-running, non-interactive jobs.

2. Freeing the Terminal While Something Runs

long_script.sh       # you forgot to start it in background
Ctrl+Z               # suspend
bg                   # resume in background
jobs                 # check status

3. Managing Multiple Long Tasks

backup.sh &
log_analysis.sh &
jobs          # see which job is which
fg %1         # watch backup progress
Ctrl+Z        # suspend when bored
bg %1         # let backup continue in background
fg %2         # check on log_analysis

4. Cleanly Stopping a Background Job

jobs
[1]-  Running                 backup.sh &
[2]+  Running                 monitor.sh &
kill %1                       # ask backup.sh to stop (SIGTERM)
jobs
[1]-  Terminated              backup.sh

Or:

fg %2
Ctrl+C                        # interrupt in the foreground

Limitations and Notes

Understanding foreground and background jobs gives you much finer control over your interactive sessions, letting you multitask effectively from the command line.

Views: 17

Comments

Please login to add a comment.

Don't have an account? Register now!