VMware ESXi and vSphere Cluster Management
Linux Background and Foreground Processes
Learn Linux shell job control: foreground and background processes, Ctrl+Z, jobs, fg, bg, job IDs, output redirection, and safe long-running command examples.
What Is a Process?
A process is an executing instance of a program. When you run a command from a Linux terminal, the shell usually starts a process to perform that command.
The shell is the command interpreter that displays a prompt, reads your input, starts programs, and manages interactive jobs. A terminal is the interactive device through which the shell receives keyboard input and displays output.
Commands started from an interactive shell normally run in the foreground. The foreground job is attached to the terminal for interactive input, so the shell does not display a new prompt until that job finishes, is stopped, or is interrupted.
Foreground Processes
In normal execution, type a command and press Enter:
sleep 60sleep waits for the specified number of seconds. During these 60 seconds, the command occupies the foreground. The shell is waiting for it, so you cannot enter another command at that prompt. When it finishes, the shell displays the prompt again.
A foreground process is a job-control state. Foreground does not mean the process has higher CPU priority, greater importance, or exclusive access to all system resources. It means that the job currently has the terminal's interactive control.
Foreground Input and Output
A foreground program can normally read input from the terminal and write output to it. The shell cannot accept its next command until control returns to the shell. Some foreground programs do not need input, but they still occupy the shell session until they finish or are stopped.
Starting a Command in the Background
Append an ampersand, &, to a command to ask the interactive shell to start it as a background job:
sleep 120 &The shell starts the command, prints job information when applicable, and immediately returns a prompt. You can then use the same terminal for another command:
$ sleep 120 &
[1] 24831
$ pwd
/home/alexThe number in square brackets, [1], is the shell's job ID. The other number, 24831 in this example, is the operating system's process ID, or PID. Exact output varies by shell and system.
Background execution does not automatically hide output. A background command can still write standard output or standard error to the terminal, which may appear between prompts or disrupt the visual layout of the shell.
Redirecting Background Output
For work that should not clutter the terminal, redirect both normal output and error output before the ampersand:
command > command.log 2>&1 &Here, > command.log sends standard output to a file, and 2>&1 sends standard error to the same destination as standard output. The final & starts the command as a background job.
Stopping a Foreground Job with Ctrl+Z
While a command is in the foreground, press Ctrl+Z to request that the terminal suspend it. Suspension pauses the job; it does not normally terminate it.
$ sleep 120
^Z
[1]+ Stopped sleep 120
$A stopped job is not doing its normal work until it is resumed. The shell reports its stopped state and gives you a prompt again.
Ctrl+Z is different from Ctrl+C. Ctrl+C sends an interrupt request to the foreground job and commonly causes the process to end. Ctrl+Z normally pauses the job so it can later continue.
Resuming Jobs with fg and bg
Resume in the Foreground
The fg shell builtin resumes a stopped job in the foreground and gives it control of terminal input:
$ fgWith no argument, fg normally selects the shell's current job. The prompt will not be available while the resumed job is in the foreground.
Resume in the Background
The bg shell builtin resumes a stopped job in the background:
$ bgThe job continues, but the shell prompt remains available. This is useful when you begin a command in the foreground, decide that it will take too long, and want to continue using the terminal without restarting the command.
Selecting a Particular Job
If several jobs exist, specify a job ID such as %1:
fg %1
bg %2The percent sign identifies a shell job specification. It is not a PID. Use jobs first when you are unsure which number belongs to which command.
Viewing Jobs with jobs
The jobs builtin lists jobs managed by the current interactive shell:
$ jobs
[1]- Running sleep 120 &
[2]+ Stopped sleep 300Common states include:
- Running: the job is executing, usually in the background when the prompt is available.
- Stopped: the job is suspended and is not proceeding normally until resumed.
- Done: the job has completed. Depending on shell settings and timing, completed jobs may be reported briefly or removed from the active job list.
The + and - markers commonly identify the current job and the previous job. Shell behavior and display details can vary.
jobs shows shell jobs, not every process running on the computer. It only knows about jobs started or tracked by that particular shell session.
Foreground, Background, and Stopped Job States
| State | Can the job run? | Does it control terminal input? | Is the shell prompt available? | How to enter or leave the state |
|---|---|---|---|---|
| Foreground | Yes | Normally yes | No, while it runs | Run normally or use fg; it leaves when it finishes, is interrupted, or is stopped |
| Background | Yes | No interactive terminal control | Yes | Append & or use bg; it can finish, stop, or be brought forward with fg |
| Stopped | No normal execution | No | Yes | Usually entered with Ctrl+Z; leave with fg or bg |
Shell Job IDs Versus Process IDs
| Identifier type | Example | Who assigns it | Where it is used | Scope |
|---|---|---|---|---|
| Shell job ID | %1 or 1 in job listings | The interactive shell | fg %1, bg %1, and job-related shell commands | Local to one shell session |
| Process ID (PID) | 24831 | The operating system | Process tools such as ps and signal commands such as kill | System-wide while the process exists |
A job can contain one process or several processes, such as a pipeline. Therefore, a shell job and an operating-system process are related concepts but are not interchangeable.
Practical Job-Control Walkthroughs
Start in the Foreground
- Run
sleep 120. - Observe that the shell does not provide another prompt while it runs.
- Press Ctrl+Z to suspend it.
- Run
jobsto confirm that it is stopped.
$ sleep 120
^Z
[1]+ Stopped sleep 120
$ jobs
[1]+ Stopped sleep 120Resume the Stopped Job in the Foreground
$ fg
sleep 120The command again controls the terminal until it completes or is stopped. Press Ctrl+Z again if you want to return to the prompt without terminating it.
Resume the Stopped Job in the Background
$ bg
[1]+ sleep 120 &
$ lsThe job continues while the shell accepts ls or other commands. Check its state with jobs.
Work with Multiple Jobs
$ sleep 120 &
[1] 24831
$ sleep 300 &
[2] 24832
$ jobs
[1]- Running sleep 120 &
[2]+ Running sleep 300 &
$ fg %1In this example, fg %1 selects the first job instead of relying on the shell's current-job default. You could use bg %2 when job 2 is stopped and should be resumed without taking the prompt.
A Long-Running Data-Copy Example
The dd utility can copy data from an input operand to an output operand. A copy may take substantial time, so it is an example of work for which background execution may be useful:
dd if=INPUT of=OUTPUT bs=1M status=progress &Replace INPUT and OUTPUT only after carefully verifying both paths. For introductory practice, use a deliberately safe regular-file destination, such as a file under /tmp, and avoid device paths. An incorrect of= value can overwrite important data, and reversing input and output operands can cause data loss.
For example, this shape uses a temporary output file but still consumes disk space, so check the size and available space before running it:
dd if=/path/to/known-input.bin of=/tmp/demo-copy.bin bs=1M status=progress > /tmp/dd.log 2>&1 &Use jobs to inspect the shell job, and redirect output when progress messages should go into a log rather than the terminal.
Interactive Job-Control Limitations
The syntax & and the builtins or key sequence Ctrl+Z, fg, bg, and jobs are primarily features of interactive shell job control. They are convenient for temporarily managing work from one terminal, but they are not a complete process-management system.
A background job may still depend on the terminal. It can receive terminal-related signals, fail when it tries to read from the terminal, or stop when the terminal session closes. A session-ending hangup signal may also affect it when you log out or close the terminal, depending on the shell and program.
For unattended work, redirect standard output and standard error, and choose a suitable persistence tool. Depending on the use case, that may be nohup, tmux, screen, or a service manager such as systemd. Use ps to inspect system processes, kill to send signals, and monitoring tools such as top or htop when broader process management is needed.
Troubleshooting
The Prompt Does Not Return
The command probably started in the foreground. Wait for it to finish, press Ctrl+Z and run bg to continue it in the background, or start a future invocation with a trailing &.
A Job Is Listed as Stopped
It may have been suspended with Ctrl+Z or received another stop signal. Run fg to resume it in the foreground or bg to resume it in the background.
fg or bg Selects the Wrong Task
With multiple jobs, the default current job may not be the one you intended. Run jobs, identify the desired number, and use fg %N or bg %N.
Background Output Overwrites the Prompt
The job's standard output or standard error is still connected to the terminal. Start it with redirection, for example:
command > command.log 2>&1 &A Job Is Not Listed by jobs
The job may have completed, or it may belong to another shell session. Remember that jobs lists only jobs tracked by the current shell. Use broader process tools when investigating processes outside that session.
A Background Command Stops After Logout
The command may depend on the terminal and receive a hangup-related signal when the session ends. For work that must survive terminal closure, use an appropriate approach such as nohup, tmux, screen, or systemd.
A dd Command Causes Data Loss
Usually, the output operand was wrong or the input and output operands were reversed. Do not practice with destructive device paths. Verify if=, of=, block size, count, and destination before pressing Enter.
Command Reference
| Command or key sequence | Purpose | Typical usage | Important behavior |
|---|---|---|---|
& | Start a command as a background job | sleep 120 & | Returns the prompt, but output may still appear in the terminal |
| Ctrl+Z | Suspend the current foreground job | Press while a foreground command is running | Pauses rather than commonly terminating the job |
fg | Resume the current job in the foreground | fg | Gives the job control of terminal input |
bg | Resume the current stopped job in the background | bg | Keeps the shell prompt available |
jobs | List jobs known to the current shell | jobs | Shows shell job numbers and states, not all system processes |
fg %job | Resume a selected job in the foreground | fg %1 | Uses a shell job specification |
bg %job | Resume a selected job in the background | bg %1 | Uses a shell job specification and leaves the prompt available |
Exam-Relevant Summary
- A process is an executing instance of a program.
- A command run normally from an interactive shell is usually a foreground job and occupies the terminal until it finishes, is stopped, or is interrupted.
- Appending
&starts a background job and returns the shell prompt. - Ctrl+Z suspends the active foreground job; Ctrl+C sends an interrupt and commonly terminates it.
fgresumes a job in the foreground, whilebgresumes a stopped job in the background.jobslists jobs for the current shell. Its job numbers are not PIDs.- Use
%1,%2, and similar specifications to select a particular shell job. - Redirect output for background work that should not write to the terminal.
- Interactive job control is not a replacement for persistent sessions, service management, or system-wide process tools.