The Linux Jobs command: list background jobs

Introduction

In the environment of the Linux command line, it is common to launch processes that continue to run as we continue to work on the terminal. These processes can stay in the foreground or move to the background, allowing us to continue typing commands without waiting for them to finish. The Jobs command is an integrated tool in most shells (such as Bash and Zsh) that shows the list of these jobs in the background, indicating their status and identification number. Knowing its use is essential to efficiently manage multiple tasks from a single session.

What's a background job?

A background work is any process that starts from the shell and is placed in running mode without blocking the standard terminal input. By adding an ampersand (&) at the end of a command, we tell the shell to run it in the background and immediately return the prompt. The works may be in different states: running, detained (e.g. by sending SITTSTP with Ctrl + Z) or completed. The shell maintains an internal work table where each one receives a work number (Job ID) other than the PID of the process, which facilitates its reference by the% symbol followed by the number.

Jobs command syntax

The basic syntax of the Jobs command is very simple:

[options] [jobID...]

If no option or jobID is specified, jobs shows all the works known to the current shell. The most common options allow you to filter the output, display the associated PID or change the display format. The most useful options and some practical examples illustrating their daily use are described below.

Most useful options

  • -l:List the PID of each job along with its job number and status. Very useful when you need to know the process identifier to send him signals with kill.
  • -n:It shows only the works that have changed status since the last notification. Ideal for scripts that want to react to changes without processing the whole list.
  • -p:It shows only the PID of each work, one per line, which facilitates its capture in pipes.
  • -r:Restrict the output to the work currently under way (running).
  • -s:It shows only the jobs that are in detention (stopped).

These options can be combined, for example,jobs -l -rshows the PID and the job number of the running processes.

Practical examples

Imagine that you are compiling a large project and want to continue editing files as the compilation progresses. You can launch the compilation in the background:

make &

Then with jobs you see something like:

[1] + 12345 running make

The number in square brackets is the Job ID (1) and the + symbol indicates the work to which it will return if fg is run without arguments. If you want to see the PID, use:

jobs -l

Output:

[1] + 12345 running make

If you send the work to the background after stopping it with Ctrl + Z, you can take it back with:

bg% 1

And to bring it back to the foreground:

fg% 1

Another typical case is to run a background development server:

python3 -m http.server 8000 &

Then you can check their status with jobs and, when you don't need it anymore, stop it with kill% 1 or take it to the foreground and finish it with Ctrl + C.

Manage works with fg and bg

The Jobs command is only ready; fg (foreground) and bg (background) are used to interact with the works. fg% n takes work n to the foreground, allowing you to interact directly with it. If the number is omitted, fg acts on the work marked with + (the last that was in the background). On the contrary, bg% n continues to work in the background. These commands are especially useful when, after pressing Ctrl + Z, you want to continue the execution without blocking the terminal. Remember that fg and bg only work on works controlled by the current shell; they do not affect processes initiated outside of it.

Good practices and advice

  • Always check the output of jobs before killing a job; using kill% n is safer than killbecause it avoids being wrong if the PID has been reused.
  • In scripts, it combines jobs -p with read to capture and act on specific work PID.
  • Avoid leaving jobs in the background indefinitely; if you no longer need them, finish them to release resources.
  • In production environments, consider using tools such as nohup or disown to separate processes from the terminal and prevent them from receiving SIghUP at the end of the session.
  • Take advantage of the -n option of jobs in monitoring loops to detect state changes without overloading the CPU.

Conclusion

The Jobs command is an essential piece of the arsenal of any Linux user working with the terminal. Its simplicity allows you to quickly obtain a clear view of the processes in the background, while their options and the combination with fg and bg provide precise control over their execution. Dominating jobs not only improves productivity by avoiding unnecessary waiting, but also contributes to a more orderly and secure management of system resources. Next time you launch a & process, remember to check jobs to keep everything under control.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish