1. Overview

The jobs command is a command-line utility that provides a snapshot of the background processes associated with current shell sessions. It allows us to monitor, control, and manipulate running processes easily.

In this tutorial, we’ll explore using the jobs command and the information it provides to manipulate our background processes effortlessly. This encompasses using the jobs command to bring a process to the foreground, suspend it, resume it, or terminate it.

2. jobs Command Options

The options used with the jobs command enable specific features or functionalities within the command that are not active by default. Moreover, options change a command’s default behavior, altering how it operates or what it produces as output.

The table below shows the common options used with the jobs command:

Options Description
-l Show process id’s in addition to the normal information
-p Shows process id’s only
-r Restrict output to running jobs only.
-s Restrict output to stopped jobs only.

The basic syntax for using the jobs command is in two forms, command-only and with options. In addition, the command takes no parameter after the options:

jobs

However, when used alone, as shown above, it simply displays a list of processes associated with the current shell session along with their status indicators.

The syntax for its usage with options and with jobs ID:

jobs jobID
jobs [ options ] jobID

Meanwhile, let’s see the meanings of each of the components of the jobs command:

  • [options]: Represents various flags and parameters to customize the behavior of the jobs command
  • [jobID]: Optional argument that allows specification of a particular job or set of jobs for the jobs command to display information about

Hence, we can proceed to explore the practical use of this command and its various options.

3. Using jobs With Practical Examples

Subsequently, let’s dive into the practical examples of using the jobs command.

3.1. Check Currently Running Jobs

For practical application, let’s start a few processes for demonstration purposes and use the jobs command to examine these processes.

These processes were run one after the other on the terminal:

$ ping cnn.com

After the ping command runs for a while, we suspend its process using the Ctrl + Z keyboard combination, then we proceed to run the next two commands consecutively:

$ sleep 600 &
$ nano Abi.txt &

Now, we can proceed to check the processes using the jobs command. Let’s start with the basic syntax without options:

$ jobs
[1]  + suspended  ping cnn.com
[2]  - suspended (tty output)  nano Abi.txt
[3]    running    sleep 3000

Furthermore, let’s understand the result of the jobs default syntax command. The numbers 1, 2, and 3 in the output are the job IDs assigned to each background job.

Moreover, the + and – signs indicate the current job that can be affected by foreground and background commands, respectively.

The two other sections of the result are the status and the commands of the processes.

3.2. Retrieve Detailed Information for All Jobs (-l)

Furthermore, the -l option prompts the jobs command to include additional information to the results output:

$ jobs -l
[1]  + 267261 suspended  ping cnn.com
[2]  - 283726 suspended (tty output)  nano Abi.txt
[3]    263562 running    sleep 600

As shown above, process IDs were included in the default results when compared with the previous results.

3.3. List Active Jobs by Specified Process ID (-p)

Let’s explore the addition of the -p option to the jobs command:

$ jobs -p %s
[3]    263562 running    sleep 3000

From the result above, the -p option prompts the jobs command to filter for any process name that starts with the string (%s) specified in the command. So, this option is used to filter and display only the process IDs of active jobs.

3.4. Retrieve Information on Running Jobs Only (-r)

Next, this option prompts the jobs command to restrict output to running jobs only:

$ jobs -r
[3]    running    sleep 700

Here, only the running process was output to the terminal.

3.5. Show Information on Jobs in a Specific State (-s)

The -s option prompts the jobs command to output processes that have been stopped:

$ jobs -s
[1]  - suspended  ping cnn.com
[2]  + suspended  sleep 600

Invariably, the processes shown above are the ones that have stopped.

3.6. Refer to Specific Job Using the % Symbol

The % filters and outputs processes using letters from the name of the process for the job specification.

For example, %p refers to the job whose process group ID matches the current process group ID of the shell.

Next, let’s use this %s to search for the latest running processes that start with the letter S:

$ jobs %s
[3]  + running    sleep 200

Moreover, a few letters from the name of the process can also be used to search:

$ jobs %sle
[1]  + running    sleep 600

The %sle argument prompted jobs to show the current process that has sle as the first three letters of its name.

4. Utilize the Output of the jobs Command

In this section, we’ll explore how to further use the output from the jobs command to manage our processes.

First, we can use the jobIDs to bring background processes to the foreground and vice versa

Additionally, the bg and fg commands are used for backgrounding and foregrounding processes.

In this instance, we can apply these commands to the previous output from our jobs command. Let’s foreground the nano Abi.txt process, which is jobID 3:

$ fg %3

The nano Abi.txt process was foreground, and the file opened on the editor in the terminal.

Next, let’s background the + suspended ping cnn.com process:

$ fg  %1
[1]  - continued  ping cnn.com
zsh: terminated  ping cnn.com

Overall, the result above shows that the process was in the background but terminated afterward.

5. Conclusion

In this article, we explored how to use the jobs command to output information about the status of the processes in our current terminal.

We also showed different ways of running the command, both with options and without options. Finally, we showed some ways we can use the command’s output.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments