System Administration
Linux Jobs command – The Basics
We can consider every command in Linux as a job that is executed. We execute most of the ordinary jobs quite fast, where the user does not need to wait long periods to regain control of the shell.
However, some jobs may require longer periods of execution. These may include moving long folders, script execution and others.
Saying that Linux provides commands that allow the manipulation of these jobs, whose status can be suspended, terminated, resumed and killed. Jobs can also be sent to the background and foreground, respectively.
This post presents how jobs manipulation occurs in Linux.
Jobs Basic Commands
To have a better understanding of Linux jobs, execute the following on your Linux terminal:
$ sleep 600
The sleep command will simply start a process and sleep for 10 minutes, while ‘locking’ the shell prompt as you do not have control over the terminal while the process is running.
Now, to regain control of the terminal, you can send the job into the background by entering the CTRZ + Z
command shortcut.
The CTRL + Z
command will put the job in the background while also stopping it. Confirm this statement by performing the previous command once again, and then performing CTRL + Z
. Once you do, perform the following command:
$ jobs
[1]+ Stopped sleep 600
The jobs
command displays all jobs currently in execution, and we can now identify our previously started job listed as stopped.
We can also start jobs and add them directly into the background, achieved by the following command:
$ sleep 700 &
Note the character &
being used, which commands the job to start on the background directly. Let’s now inspect our jobs once again.
$ jobs
[1]+ Stopped sleep 600
[2]- Running sleep 700 &
Note that we now have 2 jobs running on our jobs table, we can observe the first sleep job is stopped while the second is running.
Note job ID identifiers [1]
and [2]
, which can be used to identify the jobs on certain commands. To exemplify that, execute:
$ fg 2
The fg
command will move the job with PID 2
to the foreground. Once executed, see an output similar to:
$ fg 2
sleep 700
Notice your terminal shell locks once again while the job continues its execution in the foreground.
Now, let’s move the command again to background with CTRL+Z
and list the jobs again.
You will see something similar to:
$ jobs
[1]+ Stopped sleep 600
[2]- Stopped sleep 700
Again, CTRL+Z
sends a job to the background while also stopping it, so at this moment we have two jobs both in the stopped state.
Now, you may ask: how to resume a job in the background while keeping it in the background? We can achieve this via the bg
command.
$ bg 1
$ bg 2
Here we are using the bg
command to resume both jobs with IDs 1
and 2
, respectively. Once executed, list your jobs once again:
$ jobs
[1]- Running sleep 600 &
[2]+ Running sleep 700 &
Note that both jobs were resumed and have the Running
state, while also still being executed in the background.
Eventually, both jobs will finish their execution and when listing the jobs you will see an output something similar to:
$ jobs
[1]- Done sleep 600
[2]+ Done sleep 700
Note the Done
column, stating that both jobs were successfully finished their process.
Extra jobs functionalities
-Jobs PID
As some extra piece of useful information, you can also directly kill a job running on background by executing the following command:
$ kill -9 %[JOB_ID]
Where [JOB_ID]
is the ID of the job running in the background.
When killed, the job appears listed as Killed
as shown in the output:
$ jobs
[1]- Killed sleep 600
[2]+ Killed sleep 700
Jobs background & foreground ordering
We have previously seen how to put jobs in the background and foreground by using the bg
and fg
commands, respectively. When using those commands, we have specified the JOB_PID
to state which jobs we want to put in the background or foreground.
You can also use these commands without specifying the JOB_PID
, where the job that will be affected is the job listing with the +
sign.
To exemplify this statement, consider the following scenario:
$ jobs
[1] Running sleep 300 &
[2]- Running sleep 400 &
[3]+ Running sleep 500 &
Note that there are 3 jobs running in the background in this example. Also, note that the job with JOB_PID 3
has a +
sign on it. This means that any bg
or fg
command executed without specifying the JOB_PID
parameter will automatically target the job with the +
sign.
Let’s test it? Executing the fg
command without parameters and observe the following:
$ fg
sleep 500
Note that our job with JOB_PID 3
is the one that comes into the foreground.
As a last piece of information, let’s talk about the -
sign, which is the sign given to the job that will become receive the +
sign as soon as the job currently holding the +
sign ends.
Let’s test it? First, let’s kill the job currently holding the +
sign.
$ kill -9 %3
[3]+ Stopped sleep 500
Now, let’s list the running jobs.
$ jobs
[1]- Running sleep 300 &
[2]+ Running sleep 400 &
Observe how the job with JOB_PID 2
assumes the +
sign, after previously having the -
sign.
Now, if we use the fg
command without parameter, job 2 is the one that will come into the foreground.
$ fg
sleep 400
Summary & Cheat Sheet
Find below a small reference containing some of the main jobs commands.
jobs # list jobs from jobs table
jobs -l # list jobs from jobs table with added linux PID process information
jobs -p # list only linux PID process from the jobs
jobs -r # displays only running jobs
jobs -s # displays only stopped jobs
kill -9 %[JOB_PID] # kills a job by using its job PID instead of process PID
fg [JOB_PID] # puts a job into foreground
bg [JOB_PID] # puts a job into background
Conclusion
If you work with Linux, eventually you will need to manipulate Linux processes through the use of job tables and their commands.
This post aimed to provide the required information to give you a basic understanding of how Linux jobs work and how to manipulate them efficiently.
Linux jobs is also a topic that you will encounter if you are taking any form of Linux Certification such as the LPIC-1 or similar.
For more details, find the jobs manual pages here.
Stay tuned.
Kelson
//iamkel.devSoftware engineer. Geek. Traveller. Wannabe athlete. Lifelong student. Works at IBM and hosts the @HardcodeCast.