1

Linux中的nohup与2>&1 &

 3 years ago
source link: https://arminli.com/linux-nohup/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Armin's Blog

Linux中的nohup与2>&1 &

November 11, 2016

nohup 是 Linux 的一个常用命令,当你想要在退出账户或者关闭终端后进程仍在运行时,就可以使用 nohup 命令。nohup 就是不挂断的意思(no hang up)。

nohup 的一般形式为:

nohup command>file 2>&1 &

如果不将 nohup 命令的输出重定向,输出将附加到当前目录的 nohup.out 文件中,否则就是自己指定的文件。

尾部的&是把该命令以后台的 job 的形式运行,那么2>&1是什么意思?

基本符号与含义

  • 0 表示 stdin 标准输入
  • 1 表示 stdout 标准输出
  • 2 表示 stderr 标准错误

command>file

这个命令其实是一个缩写,实际上是 command 产生的标准输出重定向到 file 中,也就是说相当于执行了command 1 > file

2>&1

2 是标准错误,1 是标准输出,&的意思是等效于。实际就是把标准错误也重定向到 file 中,那么这样写和分别重定向有什么区别呢?

command>a 2>&1 与 command>a 2>a 的区别

经过上面的分析,command>a 2>&1这条命令,等价于command 1>a 2>&1,也就是说执行 command 产生的标准输入重定向到文件 a 中,标准错误也重定向到文件 a 中,那么是否等价于command 1>a 2>a呢?其实不是,区别在于前者只打开一次文件 a,后者会打开两次并导致标准输出被标准错误覆盖

&1的含义就可以理解为用标准输出的引用,引用的就是重定向标准输出产生打开的 a。从 IO 效率上来讲,command 1>a 2>&1command 1>a 2>a的效率更高。


Profile picture

Written by Armin Li , a venture capitalist. [Weibo] [Subscribe]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK