bash 中的 </dev/null 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19955260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is </dev/null in bash?
提问by nmax
In bash, standard (1) and error (2) output can be re-routed and discarded with:
在 bash 中,标准 (1) 和错误 (2) 输出可以通过以下方式重新路由和丢弃:
>/dev/null 2>&1
But the following example does something different:
但下面的例子做了一些不同的事情:
nohup myscript.sh >myscript.log 2>&1 </dev/null &
What is the meaning/function of </dev/null
in the example above? In what sort of scripting scenario would it be useful?
</dev/null
上面例子中的含义/功能是什么?在什么样的脚本场景中它会有用?
回答by cmh
Redirecting /dev/null
to stdin will give an immediate EOF
to any read call from that process. This is typically useful to detach a process from a tty (such a process is called a daemon). For example, when starting a background process remotely over ssh, you must redirect stdinto prevent the process waiting for local input.
重定向/dev/null
到 stdin 将立即EOF
执行来自该进程的任何读取调用。这对于将进程与 tty 分离通常很有用(这样的进程称为守护进程)。例如,当通过 ssh 远程启动后台进程时,您必须重定向 stdin以防止进程等待本地输入。
Another reason to redirect to /dev/null is to prevent an unused file descriptor being created for stdin. This can minimize the total open file handles when you have many long running processes.
重定向到 /dev/null 的另一个原因是防止为 stdin 创建未使用的文件描述符。当您有许多长时间运行的进程时,这可以最大限度地减少打开的文件句柄总数。
回答by fedorqui 'SO stop harming'
</dev/null
is used to avoid the script wait for input.
</dev/null
用于避免脚本等待输入。
Quoting from the usage of < /dev/null & in the command line:
< /dev/null
is used to instantly send EOF to the program, so that it doesn't wait for input (/dev/null
, the null device, is a special file that discards all data written to it, but reports that the write operation succeeded, and provides no data to any process that reads from it, yielding EOF immediately).&
is a special type of command separator used to background the preceding process.
< /dev/null
用于立即向程序发送EOF,使其不等待输入(/dev/null
,空设备,是一个特殊的文件,丢弃所有写入它的数据,但报告写操作成功,并且不提供任何数据给任何读取它的进程,立即产生 EOF)。&
是一种特殊类型的命令分隔符,用于使前面的进程后台运行。
So the command:
所以命令:
nohup myscript.sh >myscript.log 2>&1 </dev/null &
# | ^^^^^^^^^^^^^ ^^^^ ^^^^^^^^^^ ^
# | | | | run in background
# | | | |
# | | | don't expect input
# | | |
# | | redirect stderr to stdout
# | |
# | redirect stdout to myscript.log
# |
# keep running the command no matter the connection is lost or you logout
will move to background the command, outputing both stdout and stderr to myscript.log
without waiting for any input.
将移动到后台命令,输出 stdout 和 stderr 到myscript.log
不等待任何输入。
回答by rkh
</dev/null
provides empty input to the script...
</dev/null
为脚本提供空输入...
回答by damienfrancois
It is a way to close stdin
, as if EOF
(^D
) were sent to it. It can be used as in this example with the mail
commandto signify it that the command should no more expect input from stdin
.
它是一种关闭方式stdin
,就好像EOF
( ^D
) 被发送给它一样。它可以在本示例中与mail
命令一起使用,以表示该命令不应再期望来自 的输入stdin
。
It is also often used to daemonize processes (Stop 6 of the recommended approach for creating well-behaved daemons)
它也经常用于守护进程(创建行为良好的守护进程的推荐方法的第6 步)