Background and foreground processes

You might have noticed that, when you launch a program, it takes over your terminal, preventing you from doing other work in the terminal. This is because most programs run in the foreground when invoked from the shell.

If you have a program that takes a long time to complete, you might want to run that program in the background. To do that, simply append an ampersand (&) to the command. For example, we might want to run the dd command which can take a lot of time. If we run the command as a foreground process we won’t be able to enter any more commands in our terminal window. However, we could run the dd command as a background process:

linux background process

The dd program now runs in the background. We can use the terminal window to enter other commands.

What if a program is run as a foreground process and you want to use the terminal for something else? Well, you can suspend that program. To stop a running program and put it in the background, press Ctrl+Z. To run that program again in the foreground, type fg:

linux suspend a program

In the picture above you can see that we’ve started the dd command as a foreground process. We then suspended the program by pressing Ctrl+Z. We can now enter new commands in the terminal windows. To return the dd command to the foreground, we simply typed fg.

We could also type bg instead of fg. bg restores a job to running status, but in the background, so we can use the terminal to enter new commands.
Geek University 2021