How to Run a Linux Process in the Background

Posted on

When entering commands in a Linux terminal, we wait for the first one to finish executing before proceeding to the next. Most of the time, this doesn’t present a particular problem because the processes finish relatively quickly. For example, if you want to locate and view a file, you might have to navigate through several folders, each with its own “cd” command. These execute instantaneously, and there’s no perception of having to wait. Even commands that take longer to execute such as installations are fine most of the time. A few seconds at the most. And they can provide valuable information and prompts as well.

However, sometimes a process can take a significantly longer time to complete. In fact, it might not even be designed to finish. Perhaps it’s monitoring something and pushing its output to a log file. Or sometimes, it just takes a very long, long time to execute. One example could be code compilation which could take hours to complete. If you just let it compile, you won’t be able to use the terminal until it’s done.

If you want to be able to continue your regular work while you process a command, you need to know how to “background” a process in Linux. This quick tutorial will show you how.

Using the “&” to Background a Process

If you append the “&” symbol, any command you execute will run in the “background”. It means that while it does it’s work, you can go about your regular business and issue other commands in parallel. For example, take this command that simply adds numbers to a text file.

seq 1 1000000000 > output.txt &

If the above command had been issued without the “&” at the end, further user interaction would have been blocked until it had completed. Instead, we get an output like this:

What happens is that you get the “job number” of the background process in square brackets, and the process ID immediately after. And then the command prompt reappears, allowing you to continue your work! This is the simplest way to run a command in the background.

But what if the command is already underway?

Sending a Running Command to the Background

If you’ve already started a command and the command line is being blocked, you can first suspend the execution of the current foreground process via the signal “ctrl+z”. This immediately puts the process in a temporary freeze and shows you the job number. Like this:

After pressing ctrl+z, you can get a list of the current jobs and their process IDs by typing “jobs -l” as shown above. In this screenshot, the job ID is “1” and the process ID is “21046”.

Once you have suspended the current foreground process and know the job ID, you can manually send it into the background and resume its operating by typing:

bg 1

Where “1” is the job ID that was shown in the previous screenshot. Once it’s in the background, you can again type “jobs -l” to get its status like this:

How to "Background" a process

This time, “jobs -l” shows us that the process is “running”.

Bringing a Background Process to the Foreground

If you want, you can bring a current background job to the foreground by typing “fg [job number]” like this:

You can see that once it’s brought into the foreground, further input is blocked as expected. We need to use “ctrl+z” to suspend it once again.

Killing a Background Job

Finally, if you want to stop a background job from executing entirely, you can just kill it by specifying the job number with a “%” sign in front of it like this:

kill %1

Replace the bolded number “1” with the job number. And when you run “jobs -l” after that, it’ll tell you that it was terminated like this:

So that’s all you need to know about getting started with sending processes to the background, suspending them, bringing them to the foreground, and killing them!

Leave a Reply

Your email address will not be published. Required fields are marked *