Linux 如何将后台应用程序的输出重定向到 /dev/null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8220098/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:24:25  来源:igfitidea点击:

How to redirect the output of an application in background to /dev/null

linuxubuntu

提问by Kiran

I would like to redirect the output generated from a background application in Linux to /dev/null.

我想将从 Linux 中的后台应用程序生成的输出重定向到 /dev/null。

I am using kate text editor and it prints all the debug messages on the terminal which I would like to redirect to /dev/null.

我正在使用 kate 文本编辑器,它在终端上打印所有我想重定向到 /dev/null 的调试消息。

Any idea how to do it ?

知道怎么做吗?

Thanks

谢谢

采纳答案by evildead

You use:

你用:

yourcommand  > /dev/null 2>&1

If it should run in the Background add an &

如果它应该在后台运行,请添加一个 &

yourcommand > /dev/null 2>&1 &

>/dev/null 2>&1means redirect stdoutto /dev/nullAND stderrto the place where stdoutpoints at that time

>/dev/null 2>&1表示重定向stdout/dev/nullANDstderr到当时stdout点的地方

If you want stderrto occur on console and only stdoutgoing to /dev/nullyou can use:

如果你想stderr在控制台上发生并且只stdout/dev/null你可以使用:

yourcommand 2>&1 > /dev/null

In this case stderris redirected to stdout(e.g. your console) and afterwardsthe original stdoutis redirected to /dev/null

在这种情况下stderr被重定向到stdout(例如您的控制台),然后原始stdout被重定向到/dev/null

If the program should not terminate you can use:

如果程序不应终止,您可以使用:

nohup yourcommand &

Without any parameter all output lands in nohup.out

没有任何参数,所有输出都在 nohup.out

回答by Jim Hunziker

These will also redirect both:

这些也将重定向:

yourcommand  &> /dev/null

yourcommand  >& /dev/null

though the bash manual says the first is preferred.

尽管 bash 手册说第一个是首选。