Running Linux commands in the background

0


Linux command takes too long to finish? Use one of these six methods to send it to the background.


how to send Linux commands to the background

Linux commands are a great way to interact with the system through the terminal. However, sometimes it can take a while to complete the task at hand. This forces users to wait a considerable amount of time or create a new shell.

Fortunately, you can run Linux commands in the background by following a few simple methods. The rest of this article illustrates some of these methods.

1. Add an ampersand after your command

The easiest way to run a Linux background command is to add one Ampersands ((&) Symbol after the command. For example, if you start the gedit text editor from your terminal, you won’t be able to use the shell until you close the editor. But if you add an extra one & At your command, you can start using the shell right away.

gedit &

Run the Linux command in the background

2. Use bg to send running commands to the background

Sometimes you just run a command to find out that it takes a lot longer to exit. You can simply send these commands to the background by clicking Ctrl + Z. Keys and then with the bg Command. Ctrl + Z. stops the running process and bg puts it in the background.

Set Linux commands to background

You can view a list of all background tasks by typing the following Jobs in the terminal. Use the fg Command to return to the current task.

3. Send commands to the background with nohup

The nohup The command on Linux allows administrators to execute terminal commands that are HUP or immune hang up Signals. You can run Linux commands in the background with nohup.

In the following example, a Nmap port scan is running in the background.

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

A major benefit of nohup is that your commands will be executed even if you exit the shell. Execution log files are also generated. Search nohup.out in the current directory or in $ HOME.

Background Linux commands with nohup

4. Run background commands using system redirects

You can also run background commands on Linux using system redirects. For example when you run the following Ring Command, your shell will run it in the background and return the terminal prompt immediately.

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 all errors to the same file. The final one & signals bash to run this command in the background.

Linux command background redirect output

5. Set Linux commands to the background with disown

The deny Command on Linux makes it easy to run commands in the background. First of all, you need to send the task in the background with the & Operator. Then enter deny to detach it from your shell.

gedit &
disown

A major benefit of Disown is that, like nohup, the system does not finish your task when you close your shell or log out.

Set the background for Linux commands

6. Run Linux commands in the background with Tmux

Tmux is a powerful multiplexer that allows us to run multiple terminal sessions in a single window. Learning tmux is an excellent choice for people who are not familiar with it. Tmux makes running background commands on Linux effortless.

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

If you do the above tmux The command is executed Ring Command in a separate shell and keep it in the background. This method allows you to run any Linux command in the background.

Execute Linux commands in the background with tmux

Related: How to Install and Configure Tmux for Linux

Leave your Linux commands in the background

The ability to run commands in the background makes system management more productive for administrators. You can do your tasks in the background in a number of ways. Bash features like that & and Ctrl + Z. are convenient, but the system terminates the background job when the shell is closed. On the other hand, like tools nohup and deny Even if you log off or exit the shell, keep your command running.

Leaving your programs in the background for long periods of time can turn them into zombie processes if not properly coded. These processes can slow down the system significantly. So make sure to identify and kill zombie processes from time to time.


kill zombie processes on Linux
What are zombie processes on Linux and how do you end them?

Zombie processes are the remains of closed software. Learn how zombie processes slow down a Linux system and how to end them.

Continue reading


About the author

.



Source link

Share.

Leave A Reply