Enroll Course

100% Online Study
Web & Video Lectures
Earn Diploma Certificate
Access to Job Openings
Access to CV Builder



Online Certification Courses

How To Run Linux Commands In The Background

How to Run Linux Commands in the Background. 

How to Run Linux Commands in the Background

Linux commands provide an excellent means of communicating with the system via the terminal. However, it is possible that the task at hand will take some time to complete. This forces users to wait an extended period of time or spawn a new shell entirely.

Fortunately, by following a few simple steps, you can run Linux commands in the background. The remainder of this article will demonstrate several of these techniques.

 

1. Add an Ampersand After Your Command

The simplest way to run a Linux background command is to append the command with an Ampersand (&). For instance, if you start the gedit text editor from the terminal, you will be unable to use the shell until the editor is closed. However, by adding an additional & to your command, you'll be able to immediately use the shell.

gedit &

 

2. Use bg to Send Running Commands to the Background

Occasionally, you'll run a command only to discover that it takes significantly longer to complete. By pressing Ctrl + Z and then the bg command, you can easily send these commands to the background. Ctrl + Z terminate the currently running process, while bg places it in the background.

By typing jobs in the terminal, you can view a list of all background tasks. To return to the currently running task, use the fg command.

 

3. Send Commands to the Background With nohup

In Linux, the nohup command enables administrators to run terminal commands that are not affected by HUP or Hang Up signals. Nohup enables you to run Linux commands in the background.

The following example performs a background Nmap port scan.

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

One significant advantage of nohup is that your commands will continue to execute even if you exit the shell. Additionally, it generates execution log files. Look in the current directory or within $HOME for nohup.out.

 

4. Run Background Commands Using System Redirects

Additionally, you can run background commands in Linux by utilizing system redirects. For instance, if you execute the ping command below, your shell will execute it in the background and immediately return you to the terminal prompt.

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

The output of the ping command is routed to the output.log file in this case. If you want to discard the result, you can substitute /dev/null for it. The 2>&1 indicates to bash that any errors should be redirected to the same file. Finally, the & signals bash is used to execute this command in the background.

 

5. Set Linux Commands to the Background Using disown

In Linux, the disown command makes it simple to run commands in the background. To begin, you must use the & operator to send the task to the background. Then type disown to completely remove it from your shell.

One significant advantage of disown is that, unlike nohup, when you close your shell or log out, the system will not terminate your task. 

 

6. Run Linux Commands in the Background Using Tmux

Tmux is a powerful multiplexer that enables the simultaneous execution of multiple terminal sessions within a single window. For those who are unfamiliar with tmux, learning it is an excellent choice. Tmux simplifies the process of running background commands in Linux.

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

When you run the tmux command above, it will run the ping command in a separate shell and will keep it running in the background. This method allows you to run any Linux command in the background.

 

Leave Your Linux Commands in the Background

Having the ability to run commands in the background increases the productivity of system administrators. You can run your tasks in the background in a variety of ways. While bash features such as the & and Ctrl + Z are convenient, when the shell is closed, the system will terminate the background job. On the other hand, tools such as nohup and disown maintain the execution of your command even after you log out or terminate the shell.

If your programs are left running in the background for an extended period of time, they may become zombie processes if they are not properly coded. These processes can significantly slow the system down. Therefore, make a habit of identifying and terminating zombie processes on a regular basis.

Corporate Training for Business Growth and Schools