In Linux, a job refers to a process started and managed by the shell. That can be a single command, a long and complex shell command including pipes and redirections, an executable, or a script. Each job in Linux is managed by assigning a sequential job IP associated with a specific process.
A key concept to understand about Linux jobs is their statuses. There are two main statuses for Linux jobs:
A foreground job refers to a command or a program executed in the shell and occupies the terminal session until it completes. An example would be launching a file manager or browser in the terminal
For example, the following screenshot shows a terminal window with a foreground job.
In the above image, the shell prompt is unavailable until the Firefox window closes.
The opposite of foreground is background jobs. To initiate a job in the shell as a background job, we use the ampersand (&) symbol. Using this tells the shell to put whatever commands come before the ampersand in the background and immediately show the shell prompt.
The example below shows how to put the Firefox job (in the above example) in the background.
As you can see, the shell prompt is now available despite Firefox still running.
You will notice numerical values displayed for background jobs. The first one, indicated by square brackets (), shows the job ID, while the other value indicates the PID of the process associated with the job.
How To Manage Background Jobs
The jobs command handles job control. This allows you to view the jobs in the background.
Executing the above command shows background jobs as shown below:
Starting on the left side, we have the Job ID.
Following immediately after the brackets is the plus (+) or minus (-) sign. The plus sign indicates this is the current job, while the minus number shows the next job.
The next bracket shows the state of the job. That can be running, stopped, terminated, done, or exit with a status code.
Finally, the last part shows the actual name of the job.
Show jobs with PID
To show background jobs with their corresponding PID values, we use the -l flag as:
That will show the background jobs with their PID values, as shown in the image below.
Background jobs with output
Suppose we have a job that we want to run in the background that dumps an output on the screen. For example, in the above example, I put the apt command, which has a lot of output in the background, without messing up my terminal.
To do this, you can redirect the output in /dev/null as:
How to Bring Background Job to Foreground
We can bring background jobs to the foreground by using the fg command. For example, to bring the firefox job with Job ID of 1 to the background, we can do:
That will bring the job to the foreground as:
Jobs Command Options
The jobs command does not have a lot of options.
We have already discussed the -l to show the jobs with their process IDs.
Other options you can pass to the job command include:
- -n – This shows the jobs that have changed their status since the last notification. For example, a job that has changed from a running to a stopped state.
- -p – Lists only the PIDs of the jobs.
- -r –running jobs only
- -s – Shows only stopped jobs.
How to Terminate or Kill Jobs
We can terminate jobs using the kill command followed by either the job ID, a substring, or the process ID.
Kill using job id
To kill a job with the job ID, we use the % followed by the id value as:
This will kill the current job; this is similar to %+.
Kill a Job with a substring
Killing a job with a substring, prefix the substring with %? followed by the substring value as:
NOTE: Linux executes jobs concurrently. That means it jumps back and forth between available jobs until they complete. Hence, terminating a terminal session with jobs running will terminate all your jobs.
You do not have to worry about this if you use a terminal multiplexer like tmux or screen, as you can reattach them.
How to Kill Stopped Jobs
For us to kill all stopped jobs, we need to tie two commands together. The first will get the PIDs of all stopped jobs, and the next will kill all the jobs provided.
To view the stopped jobs, we use the command
This command shows all the stopped jobs.
Having this, we can get the PIDs of the stopped jobs and pipe them to kill command as:
This will kill all the stopped jobs.
This tutorial went over the concepts of job control in Linux and how to get information about the jobs. It is good to note that job control may not be available depending on your shell of choice.
Thank you for reading & Happy Shells.