Kelson Martins Blog


Every command in Linux can be considered as a job that is executed. Most of the ordinary jobs are executed 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 and this may include: moving long files or folders, script execution, and others.
Saying that Linux provides commands that allow the manipulation of these jobs where they can be suspended, terminated, resumed and killed. They can also be sent to background and foreground respectively.
On this post, we will see how that can be done =).

Linux Jobs Basic Commands

To have a better understanding of Linux jobs, execute the following on your Linux terminal:
[email protected]:~/Desktop$ sleep 600

The sleep command will simply start a process and sleep for 10 minutes, while ‘locking’ the shell 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 and this can be achieved through the CTRZ+Z command shortcut.
The CTRL+Z command will put the job in the background while also stopping it. Confirm this statement by entering the following command:

[email protected]:~/Desktop$ 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 and this can be achieved by the following command:
[email protected]:~/Desktop$ sleep 700 &
Note the & statement being used, which commands the job to start in the background directly. Let’s now inspect our jobs once again.
[email protected]:~/Desktop$ jobs
[1]+  Stopped                 sleep 600
[2]-  Running                 sleep 700 &
Note that we now have 2 jobs running on our jobs table where the first sleep command is still stopped while the second is running.
Note [1] and [2] on the left of the listed job, which regards the job ID, which can be used to identify the jobs on certain commands. To exemplify that, execute the following:
[email protected]:~/Desktop$ fg 2
The fg command will move the job with PID 2 to the foreground. Once executed, you should see an output similar to:
[email protected]:~/Desktop$ fg 2
sleep 700
Note that your terminal is again locked as the job continues its execution.
Now, let’s do 2 things, move the command again to background with CTRL+Z and list the jobs again.
You will see something similar to:
[email protected]:~/Desktop$ 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 2 jobs both stopped.
Now, you may ask: how to resume a job in the background while keeping it in the background
?  This can be achieved with the bg command.
[email protected]:~/Desktop$ bg 1
[email protected]:~/Desktop$ bg 2
Here we are using the bg command 1 resume both jobs with IDs 1 and 2 respectively. Once executed, list your jobs and you can see the following:
[email protected]:~/Desktop$ jobs
[1]-  Running                 sleep 600 &
[2]+  Running                 sleep 700 &
Note that both jobs were resumed and are currently listed as “Running” while also still being executed on the background.
Eventually, both jobs will finish execution and when listing your jobs you will see something similar to:
[email protected]:~/Desktop$ jobs
[1]-  Done                    sleep 600
[2]+  Done                    sleep 700
Note the “Done” column, stating that both jobs were successfully concluded.

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 on the background`
When killed, a job will be listed as “Killed” as you can see in the following output:
[email protected]:~/Desktop$ jobs
[1]-  Killed                  sleep 600
[2]+  Killed                  sleep 700
jobs background & foreground ordering
We have previously seen how to put jobs on 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 on background or foreground.
You can also use these commands without specifying the JOB_PID, where the job that will be affected is the one listed with the “+” sign.
To exemplify this statement, consider the following:
[email protected]:~/Desktop$ jobs
[1]   Running                 sleep 300 &
[2]-  Running                 sleep 400 &
[3]+  Running                 sleep 500 &
 Note that there are 3 jobs running on the background on this example. Also, note that the job with JOB_PID 3 has a “+” sign on it. This means that any bg or fg command taken without specifying the JOB_PID parameter will automatically affect the job with the “+” sign.
Let’s test it? Executing the fg command without any parameter we get the following:
[email protected]:~/Desktop$ fg
sleep 500
Note that our job with JOB_PID 3 is the one that comes into the foreground.
As a final piece of information, let’s talk about the “-“ sign, which is the sign given to the job that will receive the “+” sign as soon as the job currently holding the “+” sign terminates.
Let’s test it? First, let’s kill the job currently holding the “+” sign.
[email protected]:~/Desktop$ kill -9 %3
[3]+  Stopped                 sleep 500
Now, let’s list the running jobs.
[email protected]:~/Desktop$ jobs
[1]-  Running                 sleep 300 &
[2]+  Running                 sleep 400 &
See how the job with JOB_PID 2 assumes the “+” sign, after previously having the “-” sign.
Now, if we use the fg command without a parameter, job 2 is the one that will come into the foreground.
[email protected]:~/Desktop$ fg
sleep 400

Summary & Cheat Sheet

Please find 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`


If you work with Linux, it is certain that eventually, you will have the need of manipulating Linux processes through the use of job tables and its 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 is pretty much certain that you will encounter if you are taking any form of Linux Certification such as the LPIC-1 or similar.
For more details, you can find the jobs manual pages here.
Stay tuned.

Software engineer, geek, traveler, wannabe athlete and a lifelong learner. Works at @IBM

Next Post