Send a Process to Background Linux

17

When working with graphical desktop environments, we rarely worry about background processes. If we have a process running in the foreground, we can quickly spawn another terminal window and continue with our work.

However, if you are in a raw terminal shell such as SSH, you will often feel concerned about processes that occupy and block the shell until they are completed, especially on long-running jobs. That is where the concept of background and foreground processes comes into play.

This tutorial will discuss what background and foreground processes are, including creating and managing them in Linux.

What is a Process?

Allow me to start at the basic level: what is a process?

In Linux, a process is an instance of a program. Typically, this means any command or executable in a shell is a process.

There are mainly two types of processes:

  • Foreground processes
  • Background processes

Foreground processes are mainly typical applications that we launch and interact with them. An example would be the nautilus file manager in Gnome. In most cases, we can start foreground processes from the shell or the desktop environment.

On the other hand, background processes run in the background and require no input or interaction from the user. An example would be any typical Linux daemon.

How to Run a Process in the Background

Suppose we have a process that, while running, occupies the shell session and hinders us from executing commands until it exits.

For example, if we run the Firefox browser in the shell, it will occupy the session until process termination.


As you can see, as long as Firefox is running, the shell prompt is unavailable, and we cannot execute any more commands.

To solve this, we can do it two ways:

1: Using an Ampersand (&)

The first method is using the ampersand & sign. This tells the shell to run whatever command precedes the ampersand in the background.

An example:

In such a scenario, the process executes in the background and spawns as a new shell prompt allowing us to continue executing commands.

It also gives two numerical identifiers. The first one enclosed in square brackets is the Job ID, while the next one is the process ID.

2: Using CTRL + Z, bg command.

The next method you can use to put a process in the background is to use the shortcut CTRL + Z. This stops the process from blocking the shell. You can then use the bg command to push it to the background.

For example, start by launching Firefox as:

While the process is running, press CTRL + Z. This returns your shell prompt. Finally, enter the bg command to push the process in the background.

How to Show Background Processes

To view and manage processes in the background, use the jobs command in the shell. That will show the background jobs in the current terminal session.

For example:

An example output of background jobs:

To bring a process running in the background to the foreground, use the fg command followed by the job id.

For example, to bring the firefox job in the foreground, we use the command:

To put in the background again, press CTRL + Z followed by the bg command.

How to Make a Process Persistent After Shell Dies

When you are running processes in the background, and your shell session dies, all the processes associated with it terminate, which can be problematic, especially if it is an SSH session.

However, this is not too big an issue if you use a terminal multiplexer such as tmux or screen because, in that case, you can simply reattach the session.

However, if you run a shell session without a multiplexer, you can use the nohup command.

The nohup command is immune to hang-ups and can ignore the SIGHUP signal sent to a process.

Hence, if you run a command with nohup, it continues to run even if the shell session accidentally dies.

For example, to run Firefox with nohup, use the command:

This will run the process in the background as persist a shell terminate.

You can run a new terminal session and view the background jobs. You will see the process still running in the background.

Conclusion

In this tutorial, we discussed various ways to run and send processes to the background in Linux. We also covered how to bring a background process to the background and persist hang-up upon shell termination.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More