Detailed explanation of the use of nohup /dev/null 2>&1

Detailed explanation of the use of nohup /dev/null 2>&1

nohup command: If you are running a process and you think that the process will not end when you log out of your account, you can use the nohup command. This command can continue running the corresponding process after you log out of the account/close the terminal. nohup means don't hang up.

The general form of this command is: nohup command &

ls xxx 1>out.txt 2>&1
nohup /mnt/Nand3/H2000G >/dev/null 2>&1 &

& 1 is more accurately file descriptor 1, which generally refers to STDOUT_FILENO. This operation is actually a dup2(2) call. It outputs the standard output to all_result, and then copies the standard output to file descriptor 2 (STDERR_FILENO). As a result, file descriptors 1 and 2 point to the same file table entry, or the error output is merged. 0 represents keyboard input, 1 represents screen output, and 2 represents error output. The standard error is redirected to the standard output and then thrown into /DEV/NULL. In layman's terms, it means throwing all standard output and standard errors into the trash can.

command >out.file 2>&1 &

command >out.file redirects the output of command to the out.file file, that is, the output content is not printed on the screen, but output to the out.file file. 2>&1 redirects the standard error to the standard output. The standard output here has been redirected to the out.file file, that is, the standard error is also output to the out.file file. The last & causes the command to be executed in the background.

Think about what 2>1 means, 2 combined with > means error redirection, and 1 means that errors are redirected to a file 1, not to standard output;
Change to 2>&1, & combined with 1 represents standard output, and the error is redirected to standard output.

You can use
Test it by running ls 2>1. It will not report an error about the absence of file 2, but will output an empty file 1.
ls xxx 2>1 test, the error that there is no file xxx is output to 1;
ls xxx 2>&1 test, the file 1 will not be generated, but the error will run to the standard output;
ls xxx >out.txt 2>&1, in fact it can be changed to ls xxx 1>out.txt 2>&1; the redirection symbol > defaults to 1, and both errors and output are transferred to out.txt.

Why should 2>&1 be written at the end?

command > file 2>&1

First, command > file redirects the standard output to file, and 2>&1 is the standard error copying the standard output, that is, it is also redirected to file. The final result is that both the standard output and the error are redirected to file.

command 2>&1 >file 

2>&1 Standard error copies the behavior of standard output, but standard output is still in the terminal. >file The output is redirected to file, but the standard error remains in the terminal.

Using strace you can see:

1. command > file 2>&1
The key system call sequence to implement redirection in this command is:

open(file) == 3 
dup2(3,1) 
dup2(1,2)

2. command 2>&1 >file
The key system call sequence to implement redirection in this command is:

dup2(1,2) 
open(file) == 3 
dup2(3,1) 

Why do we use /dev/null 2>&1? This command means redirecting all standard output and error output to /dev/null, that is, discarding all generated information. Let me tell you the difference between command > file 2>file and command > file 2>&1.

First of all, command > file 2>file means to send the standard output information and error output information generated by the command to file. In this way, stdout and stderr are directly sent to file, and file will be opened twice, so stdout and stderr will overwrite each other. This is equivalent to using FD1 and FD2 to simultaneously occupy the pipe of file.

The command >file 2>&1 sends stdout directly to file, and stderr inherits the FD1 pipe and is then sent to file. At this time, file is only opened once, and only one pipe FD1 is used, which includes the contents of stdout and stderr.

From the perspective of IO efficiency, the efficiency of the first command is lower than that of the second command, so when writing shell scripts, we often use command > file 2>&1.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to use & and nohup in the background of Linux
  • How to view and close background running programs in Linux
  • In-depth understanding of the meaning of 2>&1 in Linux shell (the most comprehensive on the Internet, you will understand after reading it)
  • Linux nohup to run programs in the background and view them (nohup and &)
  • Detailed explanation of the solution to the problem of nohup log output being too large under Linux
  • Linux &, use of nohup and Systemctl
  • A brief analysis of the examples and differences of using nohup and screen to run background tasks in Linux
  • Linux nohup and tail-f usage
  • Solve the problem of python nohup linux background running output
  • PHP daemon process plus Linux command nohup to implement task execution once per second

<<:  Comparing the performance of int, char, and varchar in MySQL

>>:  Analysis and solution of MySQL connection throwing Authentication Failed error

Recommend

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

JavaScript implements H5 gold coin function (example code)

Today I made a Spring Festival gold coin red enve...

React event mechanism source code analysis

Table of contents Principle Source code analysis ...

How to implement multiple parameters in el-dropdown in ElementUI

Recently, due to the increase in buttons in the b...

JavaScript code to achieve a simple calendar effect

This article shares the specific code for JavaScr...

Can CSS be used like this? The art of whimsical gradients

In the previous article - The charm of one line o...

uniapp realizes the recording upload function

Table of contents uni-app Introduction HTML part ...

Detailed explanation of the usage of DECIMAL in MySQL data type

Detailed explanation of the usage of DECIMAL in M...

Docker implements container port binding local port

Today, I encountered a small problem that after s...

The main idea of ​​​​dynamically setting routing permissions in Vue

I have seen some dynamic routing settings on the ...

How does MySQL ensure data integrity?

The importance of data consistency and integrity ...

14 Ways to Create Website Content That Engages Your Visitors

When I surf the Net, I often see web sites filled...