M21 - Background and Foreground

Understand how foreground work, background jobs, and persistent sessions differ so long-running commands do not surprise you or die unexpectedly.

Processes

Background and Foreground

Understand how foreground work, background jobs, and persistent sessions differ so long-running commands do not surprise you or die unexpectedly.

35 min INTERMEDIATE BOTH
What you should learn
  • Understand how foreground work, background jobs, and persistent sessions differ so long-running commands do not surprise you or die unexpectedly.
Visual model

Concept to practice path

Each lesson is meant to move from an idea, to an example, to a safe practice step.

Open the full visual

Why This Matters

A long-running command can interact with your terminal in a few different ways.

If you do not understand those differences, you get common surprises:

  • a command blocks your prompt
  • a background job still prints over your work
  • a task dies when the terminal closes
  • a remote job disappears after disconnect

1. Foreground Means the Terminal Is Busy

A foreground command owns your terminal until it finishes or you interrupt it.

That is normal and often fine for short work.

Examples:

  • reading a file with less
  • running a script that takes a few seconds
  • launching a one-off command and waiting for the result

2. Background Means You Get the Prompt Back

A background job lets the command keep running while your shell prompt returns.

Start a Separate Process on Windows

Start-Process notepad

Windows often handles this by starting a separate process or window rather than by using the same job-control model as bash.

Background a Command in Bash

sleep 60 & jobs

The & starts the command in the background. The jobs command shows what your current shell is tracking.

On Linux, you can also pause a foreground job with Ctrl+Z, then resume it with bg, or bring it back with fg.


3. Background Is Not the Same as Persistent

This is the key distinction.

A plain background job may free your prompt, but it is still tied to the shell session that started it.

If that session ends, the job may receive a hangup signal and stop.

Important Distinction

"Runs in the background" and "survives disconnects or reboots" are not the same promise.


4. Persistence Tools

When you need work to survive a dropped session or be easier to resume, use a more suitable tool.

On Windows, that often means a scheduled task, a service, or a separately managed process rather than just leaving a console window open.

On Linux, common choices are:

  • nohup for simple detached commands
  • tmux or screen for persistent terminal sessions
  • systemd services for real long-lived supervised programs

The right tool depends on the kind of work:

  • quick background task: job control may be enough
  • long remote command: tmux is often better
  • real service that should restart: use a service manager
Decision guide

Choose the right running context

Action
Choose this when
Do not start here when

What to Ignore for Now

  • full shell job-control internals
  • service supervision policies in detail
  • every Windows background-process pattern

The important thing is choosing the right level of persistence.


Before You Move On

You are ready when you can explain:

  1. foreground versus background
  2. why a background job is not automatically persistent
  3. when to use job control, tmux, or a service

Next, we look at performance diagnosis with the same mindset: observe first, then intervene.

Learning loop

Recall it, check it, then practice it.

Do this before moving on. It turns reading into memory and safer action.

Recall
  • What is the difference between freeing your prompt and keeping a process alive after disconnect?
  • When is a simple background job enough, and when do you need nohup, tmux, or a service?
Check
  • Move a simple command between foreground and background without notes.
  • Explain why a plain background job may still die when the terminal session ends.

What to do next

Best next step Practice

Use the matching lab or keep building skill in practice.

Open now