VMware ESXi and vSphere Cluster Management
Jobs Command in Unix and Linux Shells
Learn how the Unix and Linux jobs command lists and manages background, foreground, and stopped jobs in the current interactive shell.
What the jobs Command Does
The jobs command displays jobs tracked by the current interactive shell. A job is a command or pipeline that the shell knows how to control, such as a command running in the background or a command suspended with Ctrl+Z.
A shell job is not the same as every process running on the computer. Programs started by another terminal, a service manager, a different shell, or a detached session normally do not appear in this shell's job list.
Job control is the shell functionality used to move commands between the foreground, stopped, and background states. It lets you temporarily suspend a command, continue it while entering other commands, bring it back to the terminal, monitor it, or terminate it.
Prerequisites: Interactive Shells and Job Control
An interactive shell reads commands from you through a terminal and normally maintains a job table. Bash, Zsh, and many other Unix shells provide job control when connected to a suitable terminal.
A foreground job is connected to terminal input and normally receives keyboard-generated signals such as the signal generated by Ctrl+C or Ctrl+Z. A background job continues while the shell accepts another command. A stopped job is suspended and does not continue until it is resumed.
Job information is normally limited to the current shell and its controlling terminal. If you open a second terminal, that terminal has a different shell and usually a different job table. Job IDs also belong to the shell session that assigned them.
Reading jobs Output
Run the command without arguments to list jobs known to the current shell:
$ jobs
[1]- Running sleep 300 &
[2]+ Stopped vi notes.txtThe number in brackets is the job ID. In this example, [1] and [2] identify jobs in this shell. A percent sign is used when referring to a job ID in commands, so job 2 is written as %2.
+current-job marker: identifies the shell's default job. Commands such asfgmay use this job when no job specification is supplied.-previous-job marker: identifies the alternate job selected by the shell.- Running: the job is currently executing.
- Stopped: the job is suspended, commonly because you pressed
Ctrl+Z. - Done: the job completed. Shells may show this notification briefly and then remove the job from the table.
The command text at the end represents the command line or pipeline that the shell is tracking. For a pipeline, the displayed text may include every component, for example producer | consumer. Exact spacing and state wording vary between shells.
Annotated Output
[1]- Running sleep 300 &
[2]+ Stopped cat | lessHere, 1 and 2 are shell job IDs, the minus and plus signs identify the previous and current jobs, the words describe job states, and the remaining text is the tracked command line.
Basic Usage
List Jobs
jobsThis lists active or retained jobs associated with the current shell. If there are no jobs, many shells produce no output and simply return to the prompt:
$ jobs
$When a background command finishes, the shell may print a completion notification immediately or at the next prompt. After notification, a later jobs command may no longer show it. This behavior differs slightly between shells and shell versions.
Start a Background Job
sleep 300 &
jobsThe ampersand asks the shell to start sleep 300 as a background job. The shell commonly prints a job ID and a process ID when it starts the command, followed by a listing such as:
[1] 24871
[1]+ Running sleep 300 &Job Specifications
A job specification is a shell reference that identifies a tracked job. Common forms include:
%1— job ID 1.%+or%%— the current job.%-— the previous job.%command— a job whose command begins with the specified prefix, where supported.%?text— a job whose command line contains the specified substring, where supported.
For example:
fg %1
bg %-
kill %?backupPrefix and substring matching are shell features. Their exact syntax, ambiguity rules, and support vary slightly between Bash, Zsh, and other shells. If a reference matches more than one job, the shell may report an ambiguity instead of choosing one.
Managing Jobs
Suspend, Resume, and Foreground a Command
Start a foreground command:
sleep 300Press Ctrl+Z to suspend it. The shell normally reports that the job is stopped. Inspect it and resume it in the background:
jobs
bg %1Use fg to return the job to the foreground:
fg %1After fg, the command again owns the terminal's input. If the job is interactive, you can continue using it normally. The job may finish, be suspended again with Ctrl+Z, or be interrupted with a keyboard signal.
Wait for a Job
wait pauses the shell until a background job finishes. It is useful in scripts and at the command line when a later command depends on the result:
sleep 10 &
wait %1
echo "The job finished"When waiting for a job, the shell can retrieve its completion status. In scripts, that status can be tested with $? immediately after wait.
Send a Signal with kill
kill accepts a job specification in shells that provide job control:
kill %1
jobsWithout a signal option, kill normally sends the default termination signal. A job specification can refer to a pipeline or process group tracked as one shell job. This differs from passing a numeric process ID, which targets an operating-system process.
Remove a Job from Shell Tracking
disown is a shell builtin available in shells such as Bash. It removes a job from the shell's job table:
disown %1Afterward, the shell no longer manages that job as one of its jobs. Disowning does not automatically make a program safe from terminal input, output errors, or every form of session termination; use an appropriate detached execution method for the workload.
Common jobs Options in Bash
Bash commonly supports these options. Other shells may omit them or give them slightly different meanings.
| Option | Effect | Example | Portability note |
|---|---|---|---|
-l | List jobs with associated process IDs. | jobs -l | Common in Bash-compatible shells, but verify the target shell. |
-p | Print process-group leader IDs for jobs. | jobs -p | Useful for PID-oriented operations; it does not print shell job IDs. |
-r | Show running jobs. | jobs -r | Supported by Bash; availability differs by shell. |
-s | Show stopped jobs. | jobs -s | Supported by Bash; availability differs by shell. |
For example, compare the normal listing with PID-oriented output:
jobs -l
jobs -pjobs -l keeps the job ID, state, command, and associated process information visible. jobs -p prints process-group leader IDs, so its numeric output should not be mistaken for [1]-style shell job IDs.
Shell Job IDs and Process IDs
A process ID (PID) is an operating-system identifier for one process. A process group is a related group of processes, often corresponding to one pipeline or shell job. A shell job ID is a short, shell-local identifier assigned for job control.
| Identifier | Example | Scope | Used by | Persistence |
|---|---|---|---|---|
| Shell job ID | %1 or [1] | Current interactive shell | jobs, fg, bg, and job specifications for kill | Can disappear when the job is removed or the shell ends |
| Operating-system process ID | 24871 | System-wide while the process exists | ps, pgrep, and PID-based signal commands | Usually reused after the process exits |
| Process group ID | 24871 | Operating-system process group | Shell job control and process-group operations | Exists while the group exists |
Shell Job States
| State | Meaning | How it commonly occurs | Typical next action |
|---|---|---|---|
| Running | The job is executing. | Started with & or resumed with bg or fg. | Monitor it, foreground it, wait for it, or terminate it. |
| Stopped | The job is suspended and will not continue until resumed. | Ctrl+Z or a stop signal. | Use bg to continue in the background or fg to continue in the foreground. |
| Done | The job completed, possibly successfully or unsuccessfully. | The command exited or was terminated. | Read the notification or completion status; the shell may remove it later. |
Job Control Command Reference
| Command or key sequence | Purpose | Example | Notes |
|---|---|---|---|
jobs | List jobs tracked by the current shell. | jobs | Does not list unrelated system processes. |
command & | Launch a command as a background job. | sleep 300 & | The shell remains available for more commands. |
Ctrl+Z | Suspend the foreground job. | Press the keys while a command is foregrounded. | The terminal's interactive job-control setting must support it. |
bg | Resume a stopped job in the background. | bg %1 | The job must be stopped. |
fg | Move a job into the foreground. | fg %1 | The job receives terminal input again. |
wait | Wait for a job to finish. | wait %1 | Useful for synchronization and collecting an exit status. |
kill | Send a signal to a job. | kill %1 | Signal behavior depends on the shell and target job. |
disown | Remove a job from shell tracking. | disown %1 | A shell builtin; not available everywhere. |
jobs Compared with Process-Management Tools
Use jobs for the small, shell-local view of commands started from the current interactive session. Use process-management tools when you need a system-wide or service-oriented view.
psdisplays detailed information about selected processes, including PIDs, parents, states, and command lines.pgrepsearches for processes by name or other attributes.topprovides a continuously updating process monitor.htopprovides an interactive process monitor when installed.- System service managers, such as a platform's service-management system, start, stop, restart, and monitor daemonized services. A service is not normally a job in your interactive shell.
A shell job may contain one process or several processes in a pipeline. The shell uses process groups to apply terminal foreground/background behavior to related processes. This is why a job reference can represent more than one operating-system process.
Scripts, SSH, Subshells, and Terminal Multiplexers
Job control is primarily designed for interactive shells. A script or noninteractive shell may not maintain the same job table, and a command such as jobs may produce no useful output. In scripts, explicit PIDs, wait, signal handling, and output redirection are usually more predictable.
Bash can enable job control with:
set -mThis is usually unnecessary in a normal interactive terminal. Enabling it in a script changes how process groups and asynchronous commands interact with the terminal, so use it only when the script specifically requires shell job control.
An SSH connection has its own shell and terminal context. Jobs started in one SSH session are not automatically visible in another. A subshell can also have different job-control behavior and does not provide a general-purpose shared job table for its parent shell.
Terminal multiplexers such as tmux and screen keep a terminal session available while you disconnect and reconnect. They are often more suitable than ordinary backgrounding for interactive programs.
What Happens When a Terminal Closes?
When a terminal or shell session ends, associated jobs may receive SIGHUP, the hangup signal. A job may then terminate, especially if it still depends on the terminal or the shell sends the signal during cleanup. Exact behavior depends on the shell, program, signal handling, and session configuration.
For a suitable noninteractive task, a basic approach is:
nohup long-task >long-task.log 2>&1 &
disown %1nohup adjusts handling of hangup-related termination and commonly redirects output when needed. disown removes the job from the shell's tracking. Neither command replaces proper service supervision. For reliable long-running workloads, use tmux, screen, or a system service manager according to whether the task is interactive or a background service.
Troubleshooting
jobs Shows No Jobs but a Program Is Running
- The program may have been started in another terminal or shell.
- It may be a service, daemon, or detached process rather than a job tracked by this shell.
- It may already have completed and been removed after notification.
Return to the shell that launched the command when possible. Otherwise, use ps or pgrep to search for system processes.
A Background Command Stops Unexpectedly
A background command that tries to read from the terminal may be stopped by terminal job-control rules. Terminal output can also interact with terminal settings and cause a stop in some configurations. Bring the job forward with fg if it needs interactive input, or redirect its input and output before starting it:
long-task <input.txt >output.log 2>&1 &fg or bg Reports No Such Job
Run jobs first. The job ID may be wrong, the job may have completed, or the command may be running in a different shell session. If you only have a PID, use process tools such as ps rather than a percent-prefixed job reference.
A Task Ends After the Terminal Closes
The shell or terminal may have sent SIGHUP to associated jobs. For suitable noninteractive tasks, consider nohup with explicit output redirection and, where appropriate, disown. Use tmux, screen, or a service manager when the task must survive disconnects reliably.
jobs Behaves Differently in a Script
Job control may be disabled in a noninteractive shell. Prefer explicit process IDs, wait, and signal handling for script concurrency. Consider set -m only when shell job control is specifically required.
Practical Workflow
- Start a command in the background with
command &, or suspend a foreground command withCtrl+Z. - Run
jobsto identify its shell job ID and state. - Use
bg %1to continue a stopped job in the background, orfg %1to interact with it. - Use
jobs -lwhen you need associated process information. - Use
wait %1when the shell must wait for completion. - Use
kill %1when the job should receive a termination signal. - Use
ps,pgrep,top, orhtopwhen the process is not in the current shell's job table.