Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to run the Linux command in the background and detach the process from the terminal

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to run the Linux command in the background and detach the process from the terminal", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to run the Linux command in the background and detach the process from the terminal" article.

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, simply press Ctrl+Z to stop it (here, "stop", not stop, but "pause"), and then type the command bg to continue to run as a task in the background.

Run the Linux command 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

Start a Linux process in the background

You can also use symbols directly & to run a process in the background:

$tar-czf home.tar.gz. & $jobs

Linux process information running in the background

Take a look at the following example. Although the tar command starts as a background task, the error message is still sent to the terminal, indicating that the process is still associated with the control terminal.

$tar-czf home.tar.gz. & $jobs

After exiting the console, keep the Linux process running

We will use the disown command, which is used after a process is already running and is placed in the background, and its function is to remove a shell task from the active task list of shell, so you will no longer be able to use the fg, bg commands for that task. Also, when you close the control terminal, the task will not be suspended (paused) or send SIGHUP signals to any of the subtasks.

After closing the terminal, keep the Linux process running

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

$sudo rsync Templates/* / var/www/html/files/ & $jobs$ disown-h% 1$ jobs

Put the Linux process in the background after closing shell

You can also use the nohup command, which also ensures that the process continues to run in the background after the user exits shell.

$nohup tar-czf iso.tar.gz Templates/* & $jobs

Detach a Linux process from the control terminal

In order to completely detach a program from the control terminal, it is more efficient for graphical user interface (GUI) programs such as firefox to use the following command line format:

$firefox / null & > / dev/null &

On Linux, / dev/null is a special file device that ignores all data written on it. The above command, input source and output are all sent to / dev/null.

As a closing statement, run a process connected to the control terminal, and as a user you will see the output of many lines of process data on your terminal, with error messages. Similarly, when you close a control terminal, both your processes and child processes will be terminated.

The above is about the content of this article on "how to run the Linux command in the background and detach the process from the terminal". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report