2>&1
http://www.cnblogs.com/caolisong/archive/2007/04/25/726896.html
nohup /mnt/Nand3/H2000G >/dev/null 2>&1 &
对于&
1 更准确的说应该是文件描述符 1,而1
一般代表的就是STDOUT_FILENO,
实际上这个操作就是一个dup2(2)调用.他标准输出到all_result ,然后复制标准输出到文件描述符2(STDERR_FILENO),其后果就是文件描述符1和2指向同一个文件表项,也可以说错误的输出被合并了.其中0表示键盘输入 1表示屏幕输出 2表示错误输出.把标准出错重定向到标准输出,然后扔到/DEV/NULL下面去。通俗的说,就是把所有标准输出和标准出错都扔到垃圾桶里面。
为何2>&1要写在后面?
command > file 2>&1
首先是command > file将标准输出重定向到file中, 2>&1 是标准错误拷贝了标准输出的行为,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。
command 2>&1 >file
2>&1 标准错误拷贝了标准输出的行为,但此时标准输出还是在终端。>file 后输出才被重定向到file,但标准错误仍然保持在终端。
https://en.wikipedia.org/wiki/Redirection_(computing)
0 stdin Standard input
1 stdout Standard output
2 stderr Standard error
linux shell中"2>&1"含义
在计划任务中经常可以看到。例如我们公司的计划任务举例:
*/2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testOne >/dev/null 2>&1 */2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testTwo >/dev/null 2>&1
对于& 1 更准确的说应该是文件描述符 1,而1标识标准输出,stdout。
对于2 ,表示标准错误,stderr。
2>&1 的意思就是将标准错误重定向到标准输出。这里标准输出已经重定向到了 /dev/null。那么标准错误也会输出到/dev/null
可以把/dev/null 可以看作"黑洞". 它等价于一个只写文件. 所有写入它的内容都会永远丢失. 而尝试从它那儿读取内容则什么也读不到.
偶尔也可以把 & 在命令的最后加上,表示让程序后台执行。
为何2>&1要写在后面?
index.php task testOne >/dev/null 2>&1
我们可以理解为,左边是标准输出,好,现在标准输出直接输入到 /dev/null 中,而2>&1是将标准错误重定向到标准输出,所以当程序产生错误的时候,相当于错误流向左边,而左边依旧是输入到/dev/null中。
可以理解为,如果写在中间,那会把隔断标准输出指定输出的文件
你可以用
ls 2>1测试一下,不会报没有2文件的错误,但会输出一个空的文件1;
ls xxx 2>1测试,没有xxx这个文件的错误输出到了1中;
ls xxx 2>&1测试,不会生成1这个文件了,不过错误跑到标准输出了;
ls xxx >out.txt 2>&1, 实际上可换成 ls xxx 1>out.txt 2>&1;重定向符号>默认是1,错误和输出都传到out.txt了。
About Redirection
Short description
Explains how to redirect output from PowerShell to text files.
Long description
By default, PowerShell sends its command output to the PowerShell console. However, you can direct the output to a text file, and you can redirect error output to the regular output stream.
You can use the following methods to redirect output: 3种方式
-
Use the
Out-File
cmdlet, which sends command output to a text file. Typically, you use theOut-File
cmdlet when you need to use its parameters, such as theEncoding
,Force
,Width
, orNoClobber
parameters. -
Use the
Tee-Object
cmdlet, which sends command output to a text file and then sends it to the pipeline. -
Use the PowerShell redirection operators.
PowerShell redirection operators 这里单独说明了第三种方式
The redirection operators enable you to send streams of data to a file or the Success output stream.
The PowerShell redirection operators use the following numbers to represent the available output streams:
Stream # | Description | Introduced in |
---|---|---|
1 | Success Stream | PowerShell 2.0 |
2 | Error Stream | PowerShell 2.0 |
3 | Warning Stream | PowerShell 3.0 |
4 | Verbose Stream | PowerShell 3.0 |
5 | Debug Stream | PowerShell 3.0 |
6 | Information Stream | PowerShell 5.0 |
* | All Streams | PowerShell 3.0 |
The PowerShell redirection operators are as follows, where n
represents the stream number. The Success stream ( 1
) is the default if no stream is specified.
Operator | Description | Syntax |
---|---|---|
> |
Send specified stream to a file. | n> |
>> |
Append specified stream to a file. | n>> |
>&1 |
Redirects the specified stream to the Success stream. | n>&1 |
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2016-09-27 Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?
2016-09-27 Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
2015-09-27 积分
2015-09-27 Playing with cubes II
2015-09-27 Smallest unused ID
2015-09-27 SHA-1
2015-09-27 git clone