Method of running Linux command in the background

  • 2021-08-28 21:44:10
  • OfStack

Normally, when you run a command on a terminal, you must wait for the current command to finish before you start typing another command. This is called running commands in the foreground, or foreground processes. When a process is running in the foreground, it takes up your shell and you can interact with it through the input device.

So what should you do when one command takes a long time to run and you want to run another command at the same time? There are several options to choose from. The most obvious and straightforward option is to start a new Shell session and run commands on it. Another option is to run commands in the background.

A background process means that a process/command runs in the background after the terminal starts, and will not interact with the user.

In this article, we will discuss the background process in Linux. We will show you how to start commands in the background and keep the process until the end of the session.

Run 1 Linux command in the background

To run the command in the background, add a symbol after the command ( & ):


command &

This shell task id (enclosed in parentheses) and process ID will be printed on the terminal:

[1] 25177

You can run many processes in the background at the same time.

The background process will constantly write information on the terminal. In order to prohibit stdout And stderr Information, use the following syntax:


command > /dev/null 2>&1 &

>/dev/null 2>&1 Means that the stdout Turn /dev/null , and stderr Turn sdtout .

Use jobs Command displays the status of all stopped and background tasks in the current shell session.


jobs -l

The output includes the task id, the process ID, the task status, and the command to start the task:


[1]+ 25177 Running   ping google.com &

To move the daemon process to the foreground, use the fg Command:

fg

If you have multiple tasks in the background, please add after the command stdout0 + Task ID:


fg %1

To stop the daemon, click kill Add the process ID after the command:


kill -9 25177

Move foreground process to background

Want to move 1 foreground process to the background:

01. Stop the current process by pressing Ctrl+Z

02. Move the stopped process to the background by entering bg

Keep the daemon running until Shell exits

If you lose your connection, or if you exit an Shell session, the daemon will be terminated. There are many ways to keep the process running until the Shell of the interaction ends.

One way is to remove the task from Shell task control and use the built-in disown :


disown

If you have many background tasks, please add after the command stdout0 + Task ID:


disown %1

By using jobs -l To confirm that the task has been removed from the task table. To list the running processes, use the ps aux Orders.

Another way to keep the process running until Shell exits is to use the nohup .

nohup Command followed by another program as an argument, will ignore all SIGHUP (Suspend) Signal. SIGHUP The signal is sent to the process to inform the terminal that it has been shut down.

Use nohup Command to run the command in the background, enter:


nohup command &

Command output will be redirected to stderr1 Files.


nohup: ignoring input and appending output to 'nohup.out'

If you log out or close the terminal, the process will not be terminated.

Alternatives

Some programs allow you to have multiple non-interactive sessions at the same time.

Screen

Screen or GNU Screen is a terminal multiplexer that allows you to open a screen session and open any number of windows (virtual terminals) in the session. Processes that run in Screen will run even if the window is not visible or even if you lose your connection.

Tmux

Tmux is a modern GNU screen interaction program. With Tmux, you can create a session and then open multiple windows in the session. The Tmux session is persistent, which means that even if you shut down the terminal, the programs running on the Tmux will still run.

Summarize

To run the command in the background, add a symbol after the command &

When you run a command in the background, you can run another command without waiting for it to end.

The above is in the background running Linux command method details, more about the background running Linux command information please pay attention to other related articles on this site!


Related articles: