nohup python3 test.py >/dev/null 2>&1 &
Linux shell中有三种输入输出,分别为标准输入,标准输出,错误输出,分别对应0,1,2。我们可以直接通过输出重定向>(或>>,表示追加)将某种输出重定向到其他地方,如设备,文件,比如:
ls > ls.log #标准输出重定向
ls 2> ls.log #标准错误重定向
ls > /dev/null #重定向到null设备,相当于直接忽略输出
但是,有时候,我们想把标准输出以及错误输出一起重定向某个文件,这是可以通过 2>&1 实现,也可以通过两个同时重定向到某个文件
ls >ls.log 2>&1 //标准输出重定向到ls.log,而错误又重定向到标准输出,这两个位置不可换
ls 2>>ls.log 1>>ls.log
先说一下linux重定向:
0、1和2分别表示标准输入、标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出。
在一般使用时,默认的是标准输出,既1.当我们需要特殊用途时,可以使用其他标号。例如,将某个程序的错误信息输出到log文件中:./program 2>log。这样标准输出还是在屏幕上,但是错误信息会输出到log文件中。
另外,也可以实现0,1,2之间的重定向。2>&1:将错误信息重定向到标准输出。
Linux下还有一个特殊的文件/dev/null,它就像一个无底洞,所有重定向到它的信息都会消失得无影无踪。这一点非常有用,当我们不需要回显程序的所有信息时,就可以将输出重定向到/dev/null。
如果想要正常输出和错误信息都不显示,则要把标准输出和标准错误都重定向到/dev/null, 例如:
# ls 1>/dev/null 2>/dev/null
还有一种做法是将错误重定向到标准输出,然后再重定向到 /dev/null,例如:
# ls >/dev/null 2>&1
注意:此处的顺序不能更改,否则达不到想要的效果,此时先将标准输出重定向到 /dev/null,然后将标准错误重定向到标准输出,由于标准输出已经重定向到了/dev/null,因此标准错误也会重定向到/dev/null,于是一切静悄悄:-)
由于使用nohup时,会自动将输出写入nohup.out文件中,如果文件很大的话,nohup.out就会不停的增大,这是我们不希望看到的,因此,可以利用/dev/null来解决这个问题。
nohup ./program >/dev/null 2>log &
如果错误信息也不想要的话:
nohup ./program >/dev/null 2>&1 &
cmd1 |cmd2 : pipe,将cmd1 的标准输出作为cmd2 的标准输入
>file :将标准输出重定向到file
><file :将file作为标准输入
>
>>file :将标准输出重定向到file,如果file存在,append到文件中,即附加到文件的后面,而不是覆盖文件
>>|file :强制将标准输出重定向到file,即使noclobber设置。当设置环境变量set –o noclobber,将禁止重定向到一个已经存在的文件中,避免文件被覆盖。
>>n >|file :强制将文件描述符n重定向到file,即使noclobber打开
>><>file :将file作为标准输入和标准输出。通常用于设备文件(/dev下面的文件),底层系统程序员可用之测试设备驱动,其他的很少用。
>>n <>file :将file作为文件描述符n的输入和输出
>><<label :Here-document; see text 。将shell的标准输入作为命令的输入,直到行中之包含label。这中间的输入成为here-document。下面是一个例子。我们让人使用cat >> file的方式,通过标准输入在文件中附加内容。
>>n>file :将文件描述符n重定向到file
>>n :将file作为文件描述符的输入
>>n>>file :将文件描述符n的输出重定向到file,如果file存在,将输出append到文件后面
>>n>& :将标准输出复制到文件描述符n(Duplicate standard output to file descriptor n)
>>n<& :从文件描述符n复制标准输入(Duplicate standard input from file descriptor n)
>>n>&m :文件描述字n将一个copy至文件描述字m(File descriptor n is made to be a copy of the output file descriptor)
>>n<&m :文件描述字n作为文件描述字m中的一个拷贝(File descriptor n is made to be a copy of the input file descriptor)
>>&>file : 将标准输出和标准错误输出定向至文件file
>><&- : 关闭标准输入
>>&- : 关闭标准输出
>>n>&- : 关闭文件描述字作为输出(Close the output from file descriptor n)
>>n<&- :关闭文件描述字作输入(Close the input from file descriptor n)
>>n>&word: If n is not specified, the standard output (file descriptor 1) is used. If the digits in word do not specify a file descriptor open for output, a redirection error occurs. As a special case, if n is omitted, and word does not expand to one or more digits, the standard output and standard error are redirected as described previously.
>>n<&word : If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to -, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.
>>n>&digit- : Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is not specified.
>>n<&digit- : Moves the file descriptor digit to file descriptor n, or the standard input (file descriptor 0) if n is not specified. digit is closed after being duplicated to n.
————————————————
版权声明:本文为CSDN博主「Eddie-Wang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wangchaox123/article/details/90670352