Linux commands are a great way to interact with the system using the terminal. However, sometimes it can take a while to finish the task at hand. This forces users to either wait for a considerable time for the process to finish or spawn a new shell altogether.

Luckily, you can run Linux commands in the background by following some simple methods.

1. Add an Ampersand After Your Command

The easiest way to run a Linux background command is to add an Ampersand (&) symbol after the command. For example, if you start the gedit text editor from your terminal, you can not use the shell until you close the editor. However, when you add an extra & to your command, you'll be able to use the shell immediately.

          gedit            &                  
run linux command in background

2. Use bg to Send Running Commands to the Background

Sometimes you run a command only to find out it takes much longer to finish. You can easily send such commands to the background by hitting the Ctrl + Z keys and then using the bg command. Hitting Ctrl + Z stops the running process, and bg takes it to the background.

set Linux commands to background

You can view a list of all background tasks by typing jobs in the terminal. Use the fg command to get back to the running task.

3. Send Commands to the Background With nohup

The nohup command in Linux allows admins to run terminal commands that are immune to HUP or Hang Up signals. You can easily run commands in the background on Linux using nohup.

The below example runs a simple Nmap port scan in the background.

          nohup sudo nmap -sS            --top-ports=15 192.168.1.1/24                  

One key benefit of nohup is that your commands will run even if you exit the shell. Moreover, it generates log files of the execution. Look for nohup.out in the current directory or inside $HOME.

background Linux commands with nohup

4. Run Background Commands Using System Redirects

You can also run background commands in Linux using system redirects. For example, if you run the below ping command, your shell will run it in the background and immediately give the terminal control back to you.

          ping -c5 8.8.8.8            >output.log 2>            &1            &                  

Here the output of the ping command is redirected to the output.log file. You can replace it with /dev/null if you want to discard the result. The 2>&1 tells Bash to redirect any errors to the same file. The final & signals Bash to run this command in the background.

Linux command background redirect output

5. Set Linux Commands to Run in the Background Using disown

The disown command makes it easy to run processes in the background. First, you need to send the task to the background using the & operator. Then, type disown to detach it from your shell.

          gedit            &            
disown

One major advantage of disown is that, like nohup, the system won't kill your task when you close your shell or log out.

set Linux commands background

6. Run Linux Commands in the Background Using tmux

tmux is a powerful multiplexer that allows you to run multiple terminal sessions within a single window. Learning how to use and configure tmux on Linux is an excellent choice for people who are unfamiliar with it. tmux makes running background commands in Linux effortless.

          tmux new -d 'ping -c 10 8.8.8.8 > output.log'        

When you run the above tmux command, it will execute the ping command in a separate shell and keep it running in the background. You can start any Linux process or command in the background using this method.

run-background Linux commands with tmux

Leave Your Linux Commands in the Background

Having the ability to run commands in the background makes system management more productive for admins. You can send your tasks to the background in several ways. Bash features like the & character and Ctrl + Z are convenient, but the system will kill the background job when the shell closes.

On the other hand, tools like nohup and disown keep your command running even when you log out or terminate the shell.

If you leave your programs in the background for a long time, they may become zombie processes if not coded properly. These processes can slow down the system significantly. So, make sure to identify and kill zombie processes every once in a while.