In Linux, any program that is running is called a process.
When we start an operating system, be it Windows or Linux, some programs are started by default.
In a Linux environment, each process is assigned a process ID or PID, used for many purposes, such as killing a process.
A process can be running in the foreground, or it could be running in the background.
In this tutorial, we will learn the fundamentals of foreground and background processes.
Table of Contents
- Access to a Linux machine (I’ll be using Ubuntu 20.04)
- Acting as a non-root sudo user to ensure a secure environment
The Types of Processes
When a program runs in the Linux environment, it starts a process, whether started automatically or manually.
As long as the program runs, the process is alive. So, essentially, there can be two types of processes in a Linux environment.
The first type is the foreground process. The second type is the background process. The difference between both the processes is the way they run. The output of both types of processes is still the same.
For example, whether you start a Web Server as a foreground or background process, it will start the Web Server, which is the outcome.
However, the command for foreground and background processes have a minor difference.
Such processes run independently.
Examples of such processes are programs and commands. For example, when you execute a command in the terminal window, it is the foreground process. The output is displayed on the terminal. One important point is that the terminal cannot be used until the command has finished running.
The background processes run in the background, which means that the terminal is free to execute other commands in parallel. The command is executed in the same manner, but we have to place an ampersand (&) at the end of the command. When the command is executed, the terminal window is free to execute more commands in parallel.
Executing Foreground and Background Processes
Let’s first start a foreground process. To do this, we execute the sleep command:
The sleep command suspends the execution of the next command for the defined number of seconds, which is 50 in this case. We will rarely run it as an independent command but consider that if we have a script and the output of one command needs to be the input of another command. We can use the sleep command and suspend the execution of the second command for a specific number of seconds.
One of the important observations to make is that the terminal is no longer free. An independent process is now running, but it has taken over the terminal window.
We will be able to use it only when this process is terminated.
After the command execution is completed, we get the control back on the terminal window. However, we can always terminate the foreground process by pressing the Ctrl + C shortcut keys.
So, this was our first foreground process.
Let’s now run the same command but as a background process. We need to add an ampersand (&) at the end of the command. Let’s see what happens when we do this.
sleep 100 &
A few quick observations:
- We get the terminal control back immediately
- A process ID is generated as it would have been done for the foreground process
- A number is assigned to the process that is created
So, how do we find out whether the process is running? We need to execute the jobs command:
Notice that the output displays the sleep command with an ampersand (
&). It has the Running status marked. At the start of the command, we also have a number.
So, does this number have significance? Yes, it does, and we will learn about it going forward.
Let’s wait for about a minute and run the jobs command once again. So, what has changed in the output? It is only the status. Instead of Running, it is now marked as Done.
Let’s now run the command once again, but we increase the timer value to 500.
sleep 500 &
A new process ID is now created, and a number is assigned to the process – this number is mentioned in square brackets.
Let’s now run the jobs command once again:
The output displays that the sleep process is running in the background.
What happens if we want to bring a background process to the foreground? We can do this with the
fg command. We need to use the number that was displayed in the output when we ran the jobs command.
Notice that the process is now running in the foreground. It has taken over the terminal. We will need to either wait for the command execution to complete or terminate it with the Ctrl + C shortcut keys.
After the defined time, 500 seconds, we get the control back on the terminal window.
Well done. Hopefully, this tutorial helped you understand some of the basics of working with foreground and background processes. If you encountered any issues, please feel free to leave a comment or contact us, and we’ll get back to we as soon as we can.