How to use jobs command in Linux

Similar to other operating systems, Linux can handle multiple tasks simultaneously. Although the single processor of your system can only run one job at a time; however, the Linux kernel manages to assign a turn to each job, which gives the impression that they are running at the same time. Various job control commands are used in Linux-based systems, and “jobs” is the one that is mainly used. This write-up will guide you to use the jobs command in Linux. Moreover, the procedure of starting a job, running it in the background, then moving it to the foreground will be provided. So, let’s start!

What is jobs command in Linux

A “job” is a set of instructions or a unit of work that is assigned to an operating system for execution. In Linux, the “jobs” command permits you to interact with the system jobs through the terminal directly. This command is also used to check the states of the jobs and list them with their detailed information. Only the bash, ksh, csh, and tcsh shells support “jobs” command execution.

States of jobs in Linux

A job in a Linux-based system can be found in one of these given states:

Foreground: A job is in a foreground state when it occupies the Linux terminal until its completion.

Background: A job is in a background state when it runs in the system background.

Stopped: A job gets into the stopped state when you execute a “stop” command for a background job or press “CTRL+Z” for a foreground job.

Syntax of jobs command in Linux

Check out the below-given syntax of the “jobs” command:

$ jobs

Or:

$ jobs [jobID]

Or:

$ jobs [Options] [jobID]

Options of the jobs command in Linux

Here is the list of options you can add in the “jobs” command:

Option Description
-lThe “-l” option is utilized in the “jobs” command to list the jobs’ additional information, such as their Process IDs.
-pThe “-p” option is utilized in the “jobs” command for only listing the Process IDs of the jobs.
-nThe “-n” option is utilized in the “jobs” command for listing the jobs that have changed their state.
-sThe “-s” option is utilized in the “jobs” command for restricting the “jobs” command to only list the stopped jobs.
-rThe “-r” option is utilized in the “jobs” command for restricting the “jobs” command to only list the running jobs.

How to use jobs command for checking jobs status in Linux

The terminal tracks the jobs whether they are running or have stopped and stores their status in a table. The primary purpose of the “jobs” command is to show the status of jobs in the Linux terminal. For instance, to check the job status table of our system, we will execute the simple “jobs” command in the following way:

$ jobs 

The output states that currently, two jobs are running, and the remaining are stopped:

Note: Now, we have terminated “gnome-calculator &” and the “inkscape &” jobs for the demonstration purpose, so that we can show you how to run both of these jobs in the next section.

How to run a job in the background in Linux

Want to start a job and run it in the system background? Yes, you can perform this operation in your Linux terminal. For running a job in your system background, write out the command and add an ampersand (&) sign at the end of it:

$ [Command] &

For instance, to run the “Inkscape” application in the background, we will type “inkscape &” and then hit “Enter”:

$ inkscape &

Now execute the “jobs” commands with the “-n” option to check the status of the job which have changed since the last notification:

You can utilize the “Command &” format for running any application or command in the background of your Linux system. For instance, the below-given command will start a new “Calculator” job:

$ gnome-calculator &

Again execute the “jobs” command with the “-n” option to check the changes added to the job status table:

$ jobs -n

How to use jobs command for listing jobs in Linux

The jobs command also lets you view the detailed information of the system jobs. This information comprises jobID, process ID (PID), jobs name, and status. Now add the “-l” option in the “jobs” command for listing the jobs with the specified information:

$ jobs -l

You can also retrieve specific entries of the jobs table based on job names. For instance, you want to view the PID, the state of the jobs whose name starts with the “i”. To do so, write out the “jobs” command and then add “i” with the “%” character in the following way:

$ jobs %i

How to use jobs command for displaying the running jobs in Linux

The “-r” option of the “jobs” command restricts it to output only the running jobs of your system:

$ jobs -r

From the below-given output, you can see that the “inkscape &” and “gnome-calculator &” jobs are currently running:

How to use jobs command for displaying the stopped jobs in Linux

To only list the stopped system jobs, add the “-s” option in the “jobs” command and then execute in the Linux terminal:

$ jobs -s

How to resume a job in Linux

The “bg” is an acronym for “Background”. The “bg” command is used for resuming the suspended jobs and running them in the background by adding a “%” character at the end of the bg command:

$ bg [%job]

In the following example, we will resume the “sudo systemctl status cron.service &” job by adding its jobID “1” in the bg command:

$ bg %1

How to move a job to foreground in Linux

The “fg” is an acronym for “Foreground”. The “fg” command brings a background job into the foreground through the Linux terminal. You can use the jobID to specify the job you want to run in the foreground. In case if you have not typed out any jobID, the “fg” command will consider the most recent  suspended job and then place it in the background:

$ fg [job%]

To move our Inkscape job to the foreground, we will add the jobID “2” in the following fg command:

$ fg %2

How to terminate a job in Linux

Now, the Inkscape job will be running into the foreground. For terminating this job, press “CTRL+ALT+Z” to stop it:

Next, specify the jobID in the “kill” command to complete the procedure of terminating the job:

$ kill %2

Conclusion

Learning job control permits you to handle several tasks simultaneously. Job control is defined as the capability to suspend or halt the executions of jobs and resume their execution as needed. In Linux, “jobs” commands are used for this purpose, and it allows having a direct interaction with the specified job through the terminal. This write-up has discussed jobs commands, its syntax, jobs states, and jobs command options. Moreover, the procedures of using “jobs” commands for listing the jobs and checking their states are also provided.