How to Use the jobs Command on Linux

In this tutorial, we will be exploring how to use the jobs command on a Linux-based system.

jobs command on Linux

The jobs command allows you to display the status of jobs started within the current terminal session.

In Linux, a job is a process that the shell is managing and hasn’t finished running. Every job has a unique ID that you can use to control it regardless of whether it is in the background or foreground of your terminal session.

By using the jobs utility, you can retrieve a list of all currently running jobs. You will be able to quickly find the ID for each job and its current status.

Alongside the bg command and the fg command, these all make up the jobs control system within Linux shells.

Note that not all shells on Linux implement this command. You must be using either the csh, bash, tcsh, or ksh shell. The majority of Linux distributions utilize the bash shell.

In the following sections, we will show you how to use this command to list jobs and identify the information it provides you.

Table of Contents

Syntax of the jobs Command

The jobs command follows a straightforward syntax and has a relatively limited number of options.

jobs [OPTIONS] [JOBID]

This command has two optional parameters that can control the output from the jobs utility.

First, the “[OPTIONS]” parameter is used to control the data presented to you and the jobs that the command will show.

The optional parameter “[JOBID]” allows you to retrieve information about a specific job by specifying its ID or name.

Basic Usage of the jobs Command

Using the jobs command without any options is the main way that you will use this utility. This command will simply print out all currently available jobs when used without any options.

All you need to do is run the following command within your terminal to list all jobs.

jobs

If you have any jobs that haven’t ended, you will end up with a result like we have shown below. However, nothing will be displayed in the terminal if you have no jobs.

pi@pimylifeup:~ $ jobs
[1]   Stopped                 ping google.com
[2]   Running                 chromium-browser &
[3]-  Stopped                 nano exampletext.txt
[4]+  Stopped                 ./examplescript.sh

Breakdown of the jobs Command Output

From our example result, let us take a single line that shows us all the jobs command data.

[3]-  Stopped                 nano exampletext.txt

Let us break down the information provided in this line. There are four different elements that we can explore.

Job Number

The first element in this string contains the job number. This is the number that is encased within the square brackets at the start of the line.

[3]

In our example, you can see the text “[3]“. Furthermore, within this text you can see that the job number for this command is “3“.

Current

The next element indicates the current job using a plus or minus symbol. Using these, you can tell what the current default job is and the one next in line to become the default. In our example, this is indicated by the minus symbol (-).

-
  • A plus symbol (+), identifies the job as the current default.

    When using commands such as fg or bg, this is the job they will affect when no job id is specified.

    You can reference this job id within supported commands by either using “%+” (percent sign, plus symbol) or “%%” (two percent signs).
  • A minus symbol (-) identifies that the job is next to become the default. When the current default exits, this will become the default job.

    You can directly reference this job id in supported commands using “%-” (percent sign, minus symbol).
  • If there is only a blank space ( ), this is just a standard job. However, it may become the default once a spot becomes available.

    Only one job can be designated by the plus symbol (+), and only one can be designated by the minus symbol (-).

State

The third element shows you the current state of that specific job. There are eight different statuses that could be set for a job.

Stopped

Below is a list of possible statuses that can appear within this field.

  • Running – This value indicates that the job is currently running and has not been suspended or has exited.
  • Done – The job has been completed and exited with a status of 0.
  • Done (code) – Like the above value, this indicates the job has been completed and exited. However, it has returned a non-zero exit status code. This code is displayed within the status indicator.
  • Stopped – This value indicates that the job has been suspended and not exited. No reason for the suspension is provided with this status.
  • Stopped (SIGTSTP) – This status means that the “SIGTSTP” signal was used to suspend the job.
  • Stopped (SIGSTOP) – This value indicates that the job was suspended using the “SIGSTOP” signal.
  • Stopped (SIGTTIN) – The status used here shows that the “SIGTTIN” signal was used when suspending the job.
  • Stopped (SIGTTOU) – The final value indicates that the “SIGTTOU” signal was used to suspend the job.

Command

The last element is the most straightforward one. It is simply the command passed to start this specific job in the shell.

For our example, we can see that we used the nano command was used to start this job.

nano exampletext.txt

More Information from the Jobs Command

You can tell the jobs command on Linux to provide additional information about your jobs by using a simple option. This option includes other information, such as the process ID for each job.

All you need to get this additional information is to use the “-l” option alongside “jobs“.

jobs -l

Below is an example of the result you will get after using this option.

These differences are only slight but valuable if you need the process ID for a specific job. This can be simpler than using the pidof command to find the process IDs.

pi@pimylifeup:~ $ jobs -l
[1]  25028 Stopped                 ping google.com
[2]  25031 Running                 chromium-browser &
[3]- 25313 Stopped (tty output)    nano exampletext.txt
[4]+ 25683 Stopped                 ./examplescript.sh

Only Get New Results from the jobs Command

The jobs command allows you to fetch new entries since your last call to the command. This is useful if you only want to see newly added jobs.

We only need to use the “-n” option alongside our call to jobs to achieve this.

jobs -n

If no new jobs have been started since you ran the command, you will not get any response.

Example of Only Getting New Results

For this example, we will run the jobs command a couple of times. This will show you how using the command with the “-n” option will only provide the latest jobs.

1. Let us start by just running the jobs command like normal. This way, it knows that we have seen all current jobs.

jobs

From this command, you should see something like the following.

[1]   Stopped                 ping google.com
[2]   Running                 chromium-browser &
[3]-  Stopped                 nano exampletext.txt
[4]+  Stopped                 ./examplescript.sh

2. If we run this command using the “-n” option, you will see that nothing is displayed as we made the call recently.

jobs -n

3. Now, let’s start up a new job by calling the nano text editor and pushing it to the background by using the “&” symbol.

nano test &

4. When we rerun the jobs command with the “-n” option, it should show the command just placed into the background.

jobs -n

Below you can see that since we started a new job, it has now been displayed thanks to the “-n” option.

[5]+  Stopped                 nano test

Only Display Process IDs from the Command Output

If you are only after the process IDs from currently running jobs, you can use an option with this command.

To only retrieve the process IDs of jobs, you can utilize the “-p” option. Each process ID will be separated into its own line.

jobs -p

For example, we got the following list of process IDs running this within our current shell.

pi@pimylifeup:~ $ jobs -p
25028
25031
25313
25683

Conclusion

At this point, you should now have a good understanding of how you can use the jobs command on your Linux system.

The jobs command finds tasks that you started within the current shell. While not all shells support this functionality, the most popular ones do.

This command is best used alongside the bg and fg commands. These two commands allow you to move a job to the foreground or background.

If you have any additional questions about the job command, comment below.

Be sure to check out our many other Linux command guides and Linux tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *