Linux Background and Foreground Processes
Learn how Linux foreground and background processes work, and use &, Ctrl+Z, jobs, fg, and bg to manage shell jobs.
When you run a command in Linux, your shell starts a process: a program instance currently executing under the operating system. The terminal is the interactive interface associated with your shell session. It accepts keyboard input, displays command output, and provides the shell prompt.
By default, the shell launches each command in the foreground. Job control lets you stop, resume, inspect, and move commands between the foreground and background without opening another terminal.
Processes, shells, and terminals
A shell such as Bash reads a command, starts the requested program as a process, and normally waits for that process to finish. The shell then displays a new prompt so you can enter another command.
A job is a process or process group tracked by the interactive shell for job control. A job can contain one process or, for example, several processes connected by a pipeline.
Foreground processes
A foreground process is the process currently attached to and controlling terminal input. Commands normally start this way:
long-task
While long-task is running, it occupies the shell. The shell does not normally accept another command in that terminal because it is waiting for the foreground job to exit, stop, or otherwise release control. Keyboard input is sent to the foreground job, not to a new shell command.
This behavior is useful for interactive programs and commands whose progress you want to watch. It is inconvenient for lengthy copies, builds, downloads, or other work when you need to keep using the same terminal.
Background processes
A background process runs without holding the shell prompt or terminal input in the foreground. Append the ampersand operator (&) to the command:
command &
The shell starts the command as a background job and immediately returns a prompt. It commonly prints a shell job ID and an operating-system PID, for example:
[1] 24871
Here, 1 is the shell job ID and 24871 is the process ID. Placing a job in the background does not mean it has finished. It may still be running, finish successfully, or fail after the prompt returns.
Example: start a lengthy copy in the background
dd is a low-level copying utility. The following example writes a 100 MiB test file while leaving the prompt available:
dd if=/dev/zero of=sample.img bs=1M count=100 &
You can run unrelated commands while the copy proceeds. However, output from a background job can still appear in the terminal because its standard output and standard error remain connected to the terminal.
Commands that need interactive keyboard input are usually poor candidates for ordinary background execution. Run such a command in the foreground, provide input from a file or pipeline, or use a noninteractive option if the program supports one.
Redirect background output
Redirect standard output and standard error to a log file to prevent progress messages and errors from interrupting your terminal session:
long-task > long-task.log 2>&1 &
Afterward, inspect the log with a suitable file-reading command, such as cat long-task.log. When the job completes, jobs can also show whether it ended successfully or failed; the shell may report the completion status in the terminal.
Foreground, stopped, and background job states
| Job state | Is it executing? | Can it read terminal input? | Is the shell prompt available? | Typical action |
|---|---|---|---|---|
| Foreground | Usually yes | Yes, when the program reads input | No | Wait, press Ctrl+C, or press Ctrl+Z |
| Stopped | No; execution is paused | No | Yes | Use fg or bg |
| Background | Usually yes | Generally no; terminal reads can cause a stop | Yes | Use fg, inspect with jobs, or wait for completion |
| Done | No | No | Yes | Inspect its result or remove the completed job notification |
Stopping the current foreground job
Pressing Ctrl+Z sends a terminal stop signal to the current foreground job. This suspends or stops the process; it does not automatically make the process continue in the background.
dd if=/dev/zero of=sample.img bs=1M count=100
^Z
[1]+ Stopped dd if=/dev/zero of=sample.img bs=1M count=100
After the job stops, the shell returns the prompt. The stopped process is paused and is not making progress until you resume it.
Ctrl+C has a different purpose. It sends an interrupt request to the foreground job, usually asking it to terminate. Use Ctrl+C when you want to interrupt the command, and Ctrl+Z when you want to pause it and decide what to do next.
Resuming stopped jobs
After pressing Ctrl+Z, choose whether the job should resume in the foreground or background:
fgresumes the current stopped job in the foreground. It controls terminal input again, and the shell prompt is unavailable until the job stops or exits.bgresumes the current stopped job in the background. The prompt remains available while the job continues.
Typical workflow
- Start a command normally, so it runs in the foreground.
- Press Ctrl+Z to stop it.
- Run
bgto continue it in the background, or runfgto continue it in the foreground.
long-task
# Press Ctrl+Z
bg
# The stopped job now continues in the background
fg
# The current job returns to the foreground
Listing and selecting shell jobs
Use jobs to list jobs known to the current interactive shell:
jobs
Typical states include Running, Stopped, and Done. A listing might look like this:
[1]- Running long-task &
[2]+ Stopped another-task
The number in brackets is the shell's job ID. Job IDs are commonly written with a percent sign when passed to fg or bg:
bg %1
fg %2
The plus sign usually identifies the current job and the minus sign identifies the previous job. If no job number is supplied, bg and fg normally operate on the shell's current job.
| Identifier | Managed by | Typical format | Where it is used | Scope |
|---|---|---|---|---|
| Job ID | Interactive shell | %1 | jobs, fg, and bg | Local to the current shell session |
| PID | Operating system | 24871 | Process inspection and signal-related tools | System-wide while the process exists |
A job ID and a PID are not interchangeable. The shell can track a job by %1 even though the operating system identifies its process with a number such as 24871.
Interactive job-control commands
| Command or key sequence | Effect | Example use | Notes |
|---|---|---|---|
& | Starts the preceding command as a background job | long-task & | The prompt returns, but the job may still be running |
Ctrl+Z | Stops the current foreground job | Press it while a command is active | Stopping is not the same as background execution |
jobs | Lists jobs known to the current shell | jobs | Shows states such as Running, Stopped, and Done |
fg | Resumes the current job in the foreground | fg | The shell becomes occupied again |
bg | Resumes the current stopped job in the background | bg | Requires a suitable stopped job |
fg %N | Resumes job N in the foreground | fg %2 | Uses a shell job ID |
bg %N | Resumes stopped job N in the background | bg %1 | Uses a shell job ID |
Ctrl+C | Sends an interrupt request to the foreground job | Press it to stop a command when appropriate | It requests interruption rather than suspension |
Job control scope and limitations
fg, bg, jobs, and Ctrl+Z are interactive shell job-control features. They manage jobs known to that shell, not arbitrary processes from every session.
When a terminal session closes, background jobs can receive SIGHUP, a signal often associated with terminal disconnection or shell-session termination. Whether a job exits, ignores the signal, or continues depends on the shell, the program, and session configuration.
Redirecting output prevents terminal clutter but does not by itself guarantee that work survives logout. For work that may need to continue after logout, one possible approach is:
nohup command > output.log 2>&1 &
nohup is a separate concern from interactive job control, and its behavior can vary with the shell and environment. For reliable long-running work, choose a persistent session such as tmux or screen, a service manager such as systemd, or a scheduler such as a cron job or systemd timer. Job control is not a substitute for process supervision, service management, or scheduling.
Safe use of long-running commands
Verify a command before adding &. Background execution makes the prompt available quickly, but it also makes it easier to overlook warnings or an incorrect destination.
Take special care with dd. An incorrect if source, of destination, block device, or option can overwrite data. Before running it:
- Verify every source and destination path.
- Check permissions and available disk space.
- Confirm the options and units, such as
bs=1Mandcount=100. - Practice with ordinary test files rather than block devices.
- Redirect output to a log when the command will run for a long time.
dd if=/dev/zero of=sample.img bs=1M count=100 > dd.log 2>&1 &
jobs
cat dd.log
If a risky job is already running, stop it if doing so is safe, then verify the paths and arguments before restarting. A process that has completed may appear as Done, but you should still inspect its output, error log, and exit result where available.
Troubleshooting job control
The prompt does not return
The command is probably still running in the foreground. Wait for it to finish, press Ctrl+C if interruption is appropriate, or press Ctrl+Z and then use bg if the work should continue in the background.
The command stopped making progress after Ctrl+Z
Ctrl+Z stopped the process; it did not resume it. Run bg to continue it in the background or fg to continue it in the foreground.
bg reports that there is no current job
There may be no stopped job in the current shell, the job may have completed, or the job ID may be wrong. Run jobs, find a Stopped entry, and use its percent-prefixed ID, such as bg %1.
A background command pauses while requesting input
Background jobs generally should not read from the controlling terminal. Bring the job to the foreground with fg, provide input through a file or pipeline, or use a noninteractive option.
Background output appears among typed commands
The job is still writing to standard output or standard error connected to the terminal. Start it again with redirection:
long-task > long-task.log 2>&1 &
The job disappears or stops after the terminal closes
The session may have ended and the process may have received SIGHUP or lost its controlling terminal. For work that must persist, use an appropriate method such as nohup, tmux, screen, systemd, or a scheduler.
Exam-relevant distinctions
- Foreground means the job controls terminal input and occupies the shell prompt.
- Background means the shell prompt is available while the job may still be executing.
- Stopped means execution is paused. A stopped job is not automatically a running background job.
- Ctrl+Z suspends the foreground job; Ctrl+C requests interruption.
- Job ID, such as
%1, is shell-local. A PID, such as24871, is assigned by the operating system. jobslists jobs for the current interactive shell; it is not a complete process listing for the entire system.
Related Linux topics
For more command-line foundations, see Linux and showing the full path of shell commands.