How to run _Linux_ commands in the background and take the process out of the terminal

Abstract: In this guide, we will illustrate a simple but important concept of process management in Linux systems, and that is how to completely detach a process from its controlling terminal. When a process is associated with a terminal, two kinds of problems can arise: Your controlling terminal is full of output data or errors and diagnostics. If a terminal shutdown occurs, the process and its children will be terminated. For the above two problems, you need to completely detach from a process from a controlling terminal.

In this guide, we will illustrate a simple but important concept of process management in Linux systems, and that is how to completely detach a process from its controlling terminal.

When a process is associated with a terminal, two problems can arise:

Your controlling terminal is filled with a lot of output data or errors and diagnostics.
If a terminal shutdown occurs, the process, along with its children, will
be terminated. For the above two problems, you need to completely detach from a process from a controlling terminal. Before we actually tackle this, let's briefly describe how to run a process in the background.

How to start a Linux process or command line in the background
If a process is already running, such as the tar command line example below, simply press Ctrl+Z to stop it It means "pause"), and then enter the command bg to continue running as a task in the background.

You can view all background tasks by typing jobs. However, standard input (STDIN), standard output (STDOUT), and standard error (STDERR) are still mixed into the console.

$ tar -czf home.tar.gz .
$ bg
$ jobs
run linux command
in background

You can also directly use the & symbol to run a process in the background: $ tar -czf

home.tar.gz . &
$ jobs
start a Linux process
in the background

A background task is started, but the error message is still sent to the terminal, which means that the process is still associated with the controlling terminal.

$ tar -czf home.tar.gz . &
$ jobs
Information about Linux processes
running in the background

After exiting the console, keep Linux processes running
We will use the disown command, which works when a process is already running and Used in the background, its effect is to remove a shell task from the shell's active task list, so you will no longer be able to use the fg, bg commands for that task.

Also, when you close the controlling terminal, the task will not suspend (pause) or send a SIGHUP signal to any of the subtasks.

Let's take a look at the following example using the built-in command disown in bash.

$ sudo rsync Templates/* /var/www/html/files/ &
$ jobs
$ disown -h %1
$ jobs
After closing the terminal, keep the Linux process running After
closing the terminal, keep the Linux process running

You can also use the nohup command, which also keeps processes running in the background after the user exits the shell.

$ nohup tar -czf iso.tar.gz Templates/* & $
jobs put the linux process in the background after closing the shell put the linux process in the background
after closing the shell program. For graphical user interface (GUI) programs such as firefox, it is more efficient to use the following command line format: $ firefox </dev/null &>/dev/null & On Linux, /dev/null is a A special file device that ignores all data written on it. The above command, input source and output send destination are all /dev/null. As a closing statement, run a process connected to the controlling terminal, as a user you will see many lines of output for the process data on your terminal, including error messages. Likewise, when you close a controlling terminal, both your process and child processes will terminate. The original release time is: 2017-11-01 This article is from Yunqi Community Partner "Linux China"












Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221815&siteId=291194637