How To Use Jobs Command.
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:
Options of the jobs command in Linux
Here is the list of options you can add in the “jobs” command:
The “-l” option is utilized in the “jobs” command to list the jobs’ additional information, such as their Process IDs.
The “-p” option is utilized in the “jobs” command for only listing the Process IDs of the jobs.
The “-n” option is utilized in the “jobs” command for listing the jobs that have changed their state.
The “-s” option is utilized in the “jobs” command for restricting the “jobs” command to only list the stopped jobs.
The “-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:
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:
For instance, to run the “Inkscape” application in the background, we will type “inkscape &” and then hit “Enter”:
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:
Again execute the “jobs” command with the “-n” option to check the changes added to the job status table:
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:
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:
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:
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:
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:
In the following example, we will resume the “sudo systemctl status cron.service &” job by adding its jobID “1” in the bg command:
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:
To move our Inkscape job to the foreground, we will add the jobID “2” in the following fg command:
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:
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.