bash 如何处理来自后台 linux 任务的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14630104/
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
How to deal with output from a background linux task
提问by danivicario
I have a task that is continuously echoing info.
我有一个不断回显信息的任务。
For example, if you do a git clone and you want to send that task to the background (by using ampersand)
例如,如果您执行 git clone 并且想要将该任务发送到后台(通过使用&符号)
git clone https://github.com/mrdoob/three.js.git &
then the git clone operation is constantly refreshing the screen with the new percentage of the git clone process, ie:
然后 git clone 操作不断地用 git clone 进程的新百分比刷新屏幕,即:
Receiving objects: 47% (22332/47018), 92.53 MiB | 480 KiB/s 1410/47018), 7.18 MiB | 185 KiB/s
Receiving objects: 53% (24937/47018), 99.40 MiB | 425 KiB/s 1410/47018), 7.18 MiB | 185 KiB/s
So I cannot continue doing other operations in the foreground, as these updates are preventing me to see what I am trying to write.
所以我不能继续在前台进行其他操作,因为这些更新阻止我看到我正在尝试编写的内容。
Can you tell me guys how to effectively send one verbose task like this to the background?
你能告诉我伙计们如何有效地将这样一个冗长的任务发送到后台吗?
Thanks a lot!
非常感谢!
采纳答案by nullrevolution
you could have the process write its output to a file (if you need to view it later) like this:
您可以让进程将其输出写入文件(如果您稍后需要查看),如下所示:
git clone https://github.com/mrdoob/three.js.git >output.txt &
or discard the output altogether like this:
或者像这样完全丢弃输出:
git clone https://github.com/mrdoob/three.js.git >/dev/null &
edit:
编辑:
you could also include any error messages sent to stderror in either of the above options by replacing the &with 2>&1 &
您还可以包括通过替换发送到stderror在任何上述选项的任何错误信息&与2>&1 &
回答by jman
The other answers are good, but you can also use:
其他答案很好,但您也可以使用:
git clone -q ...
回答by Emanuele Paolini
redirect its standard output:
重定向其标准输出:
git clone https://github.com/mrdoob/three.js.git > /dev/null &
or use appropriate verbose options of the command (in this case git)
或使用命令的适当详细选项(在本例中为 git)
回答by TopGunCoder
If you are specifically looking to put it in the background you can append an ampersand (&) to the end of the command, or while it is running use ctrl+zthen the command 'bg'to run it in the background.
To bring it back, use jobsto list your jobs and then fg %{job #}to bring it back.
Hope this helps and works in this unique situation
如果您特别希望将其置于后台,则可以&在命令末尾附加一个和号 ( ),或者在它运行时使用ctrl+z该命令'bg'在后台运行它。要将其带回来,请使用jobs列出您的工作,然后fg %{job #}将其带回来。希望这有助于并在这种独特的情况下起作用

