Are you learning to administer Linux? Well, one of the requirements is manipulate jobs or processes from your terminal. In this environment, determining which ones are in the foreground and in the background is vital.
Also called foreground (with respect to the first) and background (with respect to the second) each one has its own particular objectives. These types are used depending on the kind of program to be executed.
In addition to this, it can vary depending on the way you want to work. You must also add other basic commands to understand, Jobs or “” in order to list each process. Here you will learn to stop, suspend or move them like an expert.
What is a foreground process and what is it for?
When we run a program from the terminal, we usually do it in foreground. As soon as we insert its name, it will eclipse the entire console. This means that no other commands will be supported until the process is complete. This mode is useful when the programs require interaction with the user. Note that if you want to stop the current process in foreground you just press the shortcut CTRL + Z.
Be careful with this feature when tasks you are running are critical. Doing so you will already be pausing the software and you can start another one. Actually, when you enter CTRL + Z you are taking the execution to the background, but paused. The important thing is that you can take control of the console again. Finally, you may want to stop the program immediately and without pausing it.
After being sure of your decision, press CTRL + C. Or if you prefer to do it directly from the console insert the argument kill. This command must be followed by the number or ID of the program seen in the list. You will have a result similar to kill% 3. This is where the question arises How do I see the list of executed jobs? Well, keep reading and you will understand how to do it. But before What are the differences between foreground Y background?
What are the differences between a foreground process and a background process?
We know that in foreground the terminal it will not accept any additional arguments until the process is complete. On the contrary, the processes in background consist of that the user can continue to enter commands. The interpreter will accept them without problems as the execution job is hidden. This is its main difference, but it also helps the terminal not stay open. This way it will not show excessive output data, nor will there be error messages.
There will not even be the danger of the process being stopped. The latter will obviously affect the task as it has not been executed well. Also, when we decided to run the hidden program is because it requires no interaction. In this way we can save time and carry out other operations from the same console. Given the does not overshadow the terminal, is relevant for download files, scripts, and more.
With this in mind, stop start a program in the background from the beginning you have to use the argument & (ampersand). The correct way to use it is by typing the name of the software to run and then the & symbol. Let’s imagine then that our program is called “Myprogram”. So the syntax should look like $ myprogram &
. If you do it this way you will work software that may consume a lot of CPU resources. But they don’t really need any kind of human interaction.
Learn how to view a list of processes in the foreground and background in Linux
To achieve this goal, all you have to do is follow these simple steps:
- Being in the terminal you place the argument jobs, which literally means processes
- This is how it will appear to you a list of the jobs or processes that are running
- Next to its can also be reflected those that have been paused or are suspended
-
Syntax it is simple
$ jobs
Possibly something similar to this appears:
Another alternative is to use the ps (process status) thanks to this we will see the list and along with it other characteristics:
- For example, PID (Unique ID of the job run) TIME (time since launch) and CMD (specific name of the software or file) The syntax could well be
ps -A
to receive a list of all active jobs and descriptions.
If in addition to the above, you need to see the list of programs in background or do you want to bring one to the fore is simple:
- Detects which are being executed through jobs
- Write fg followed by the run ID
- For example:
fg %3
East would bring the process that has the ID or number 3 in position at foreground, which would run without interruption.
On the other hand, if what you want is to continue working a program paused in the background, use bg:
- Insert bg followed by the number of the software or paused job
- Thus you should have a result very similar to this
bg %3
.