Background and Foreground processes in Ubuntu

While working in graphical environment, we have the luxury of opening multiple in a terminal tabs at once. And, in that we can run our processes which do not affect other running processes. But, certain situations would demand something else. What if we are not provided the graphical environment at all. In that scenario, if we don’t understand the concept of background and foreground processes. Then, we need to wait for one process to finish the work. And, then we will move on to next task. So, in this article we would see how to work with background and foreground processes in Ubuntu distribution.

The solution to the above problem is to send the processes which would take time to complete to the background. It will keep running in the background. And, when the task is finished we can bring it to the foreground. After sending the process to the background, we can continue with some other process in the foreground.

And, exactly how we are going to do it will be discussed next.

Background and Foreground processes in Ubuntu

We have got two ways to send any process to the background –

First, either use ampersand (&) at the end of the command. For instance,

nano test &

It would return with something like –

[1] 3879

or, use Ctrl+Z to stop the process and send it to background –

nano test1

and, once the file opens press Ctrl+Z. Thereafter, it would return with –

[2]+ Stopped nano test1

To see the list of commands which are there in the background –

jobs -l

The output –

[1]- Stopped nano test
[2]+ Stopped nano test1

where + sign denotes the most recent background process. -l option is display Process ID (PID).

To bring a particular process to the foreground, use –

fg %[job_ID]

For instance, to bring nano test1 to the foreground –

fg %2

Run stopped processes in the background

In the above example, when we run jobs -l. The output to the process shows Stopped processes. So, it means that the processes are not running in the background. But, that kind of defeats the entire purpose. We wanted to make the process work even in the background. Therefore, we will see in this section here how to run stopped processes in the background.

Let’s say we have the following command to run –

ping <IP_Address> -n 60

And, press Ctrl+Z. We are just trying to explain the concept that’s why we chose not to provide any IP Address here.

Now, use –

jobs -l

It would return with –

[1]+ [PID] Stopped ping <IP_Address> -n 60

Now, to run the above process use the following command –

bg %[job_ID]

So, in this case it would be –

bg %1

To verify whether the process is running or not –

jobs -l

It would return with –

[1]+ [PID] Running ping <IP_Address> -n 60 &

In conclusion, we have discussed how we can utilize background and foreground processes in Ubuntu to our advantage.

Similar Posts