How to Run a Command in the Background in the Ubuntu Terminal

When you run a command in the Ubuntu terminal, it typically executes in the foreground. This means the terminal is completely locked up, displaying the output of that specific task until it finishes. If you are extracting a massive archive, downloading a large file with wget, or compiling software, you might be staring at a useless terminal window for hours, unable to run any other commands.

You do not need to open a second terminal window to keep working. Instead, you can instruct Ubuntu to run the command in the background. This instantly returns control of the prompt to you, allowing you to run other commands while the heavy lifting happens silently behind the scenes.

This guide explains how to start a new command in the background, how to move a running command to the background, and how to ensure your command keeps running even if you close the terminal.

Method 1: Start a Command in the Background (The Ampersand)

If you already know a task is going to take a long time, the easiest method is to tell the terminal to launch it directly into the background. You do this by appending a single ampersand (&) to the very end of your command.

For example, if you want to run a long Python script, you would type:

python3 long_script.py &

When you press Enter, the terminal will instantly return your prompt. It will also display a line of text that looks something like [1] 45678. The number in brackets is the job number, and the larger number is the Process ID (PID). You can now continue typing new commands freely.

Method 2: Move a Running Command to the Background

Often, you run a command expecting it to take five seconds, but it ends up taking five minutes, leaving your terminal locked. You do not have to cancel the command to get your terminal back; you can pause it and push it to the background.

  1. While the long-running command is hogging your terminal, press Ctrl + Z. This instantly suspends (pauses) the process and gives you the prompt back.
  2. To resume the paused process in the background, type the following command and press Enter:
    bg

The command will wake up and continue executing silently in the background.

How to Manage Background Jobs

Once you have pushed several tasks to the background, you might want to check on their status or bring one back to the foreground.

  • List all background jobs: Type jobs and press Enter. This displays a numbered list of everything currently running in the background.
  • Bring a job to the foreground: If you want to see the output of a background task, type fg %1 (replace 1 with the specific job number you saw when running the jobs command). This locks your terminal to that task again.

Method 3: Keep Commands Running After Closing the Terminal

There is a major caveat to the methods above: if you close the terminal window or disconnect from your SSH session, Ubuntu will automatically kill all background jobs associated with that session. This is disastrous if you are running a 10-hour backup script.

To run a command in the background that survives after you close the terminal, you must use the nohup (no hangup) command alongside the ampersand.

nohup python3 long_script.py &

When you use nohup, any text output that the command generates (which would normally print to the screen) is automatically saved to a file called nohup.out in your current directory. You can safely close your terminal, turn off your computer, or go to sleep. The server will continue executing the task in the background until it finishes.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.