Home » Linux » Shell » Linux Ubuntu Run Command Background

How To Run Commands in the Background [Linux/Ubuntu]

This article will show you how to run commands in the Linux (including Ubuntu) shell background. This is useful when working remotely via SSH.

Say you’re connected remotely via SSH to a remote computer, and you want to execute a lengthy task.

While the task is running, usually, you’d have to keep the connection open, with the terminal window open. However, this can be a hassle if you need to close the window to perform another task or if you have a spotty internet connection that will likely interrupt the connection.

If the window is closed or the connection is lost, the command may stop executing on the remote computer.

By running the command in the background, this can be avoided – it will keep running whether or not the connection/session remains open.

Sending commands to run in the background can also be useful in scripts if you wish to execute a command but do not wish to wait for it to complete and do not require the output later in your script.

Using & to Run a Command in the Background

Running a command in the background in Linux is easy – add a & character to the end of your command:

your_command_here &

For example, the following command zips up a pair of files – the command is run in the background, so you don’t have to wait for it to complete before running other commands:

zip archive.zip file1 file2 &

Easy! The output will look something like this:

[1] 3001

…indicating the job ( in this case [1]) and process ID (in this case 3001) of the command being executed in the background.

When the job has been completed (or failed), the status of the job will be output:

[1]  + done       zip archive.zip file1 file2 

Hiding the Output of the Background Job

The output from the command will continue to be printed to the terminal while it is running in the background.

If you wish to keep this hidden (i.e., it’s interrupting you performing other tasks) – you can suppress the output using standard redirection.

your_command > /dev/null 2>&1 &
  • Above, > is redirecting the STDOUT (standard output) of the command to /dev/null.
  • The STDERROR (standard error output) of the command is redirected to STDOUT using 2>&! so that STDERR goes to /dev/null as well.
  • /dev/null means the output goes nowhere. It’s gone, hidden, into nothing.

Listing Running Background Jobs

Use the jobs command to list background jobs that are currently running:

jobs -l

It will show a list of running background jobs – each entry will look something like this:

[1]  + 3601 running    ping > /dev/null 2>&1

Above, you can see the command ping > /dev/null 2>&1 is running in the background with a job ID of 1 and a process ID of 3601.

Stopping a Background Job

The kill command stops a task:

kill 3601

… supply it with the process ID of the background job, and it will stop (kill) it.

Keeping Background Jobs Running, Even if the Session is Interrupted

This is probably the most useful bit.

To allow a command to continue running, even if you close the window, or the connection drops, or the session is otherwise interrupted, you’ll need to disown the background job using the disown command.

disown %1

Above, the background job with a job ID of 1 is disowned. It is disconnected from the current shell and will continue to run until either it finishes or encounters an error or it is killed using the KILL command.

Check out this article to see how to list all running processes – you might need it if you need to track down tasks you’ve disowned.

Moving Running Commands to the Background

To move a running command into the background, you must suspend it, then move it to the background.

To do this, while the command is running, press CTRL + Z. You’ll see something like:

suspended  command_name

…confirming the command has been suspended.

Then, enter the bg command to continue running the command in the background:


You’ll see something like

continued  command_name

…confirming that the command is now running in the background.

Photo of author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment