在后台运行 bash 命令而不打印作业和进程 ID
在 bash 中在后台运行进程相当容易。
$ echo "Hello I'm a background task" &
[1] 2076
Hello I'm a background task
[1]+ Done echo "Hello I'm a background task"
但是输出很冗长。第一行打印后台任务的作业 id 和进程 id,然后是命令的输出,最后是作业 id、其状态和触发作业的命令。
有没有办法抑制运行后台任务的输出,使输出看起来与末尾没有“&”符号完全相同?即:
$ echo "Hello I'm a background task" &
Hello I'm a background task
我问的原因是我想运行后台进程作为制表符完成命令的一部分,因此该命令的输出必须不间断才能有意义。
To run a process in the background in bash is fairly easy.
$ echo "Hello I'm a background task" &
[1] 2076
Hello I'm a background task
[1]+ Done echo "Hello I'm a background task"
However the output is verbose. On the first line is printed the job id and process id of the background task, then we have the output of the command, finally we have the job id, its status and the command which triggered the job.
Is there a way to suppress the output of running a background task such that the output looks exactly as it would without the ampersand at the end? I.e:
$ echo "Hello I'm a background task" &
Hello I'm a background task
The reason I ask is that I want to run a background process as part of a tab-completion command so the output of that command must be uninterrupted to make any sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
与完成无关,但您可以通过将调用放入子 shell 中来抑制该输出:
Not related to completion, but you could supress that output by putting the call in a subshell:
基于 @shellter 的答案,这对我有用:
我不知道这背后的原因,但我记得从一篇旧帖子中 disown 阻止 bash 输出进程 ID。
Building off of @shellter's answer, this worked for me:
I don't know the reasoning behind this, but I remembered from an old post that disown prevents bash from outputting the process ids.
在某些较新版本的 bash 和 ksh93 中,您可以用子 shell 或进程组包围它(即
{ ... }
)。In some newer versions of bash and in ksh93 you can surround it with a sub-shell or process group (i.e.
{ ... }
).基于这个答案,我想出了更简洁和正确的:
Based on this answer, I came up with the more concise and correct:
基于上面的答案,如果您需要允许 stderr 从命令中通过:
Building on the above answer, if you need to allow stderr to come through from the command:
尝试:
并且您已经隐藏了输出和PID。请注意,您仍然可以从 $REPLY 检索 PID
Try:
And you have hidden both the output and the PID. Note that you can still retrieve the PID from $REPLY
很抱歉回复旧帖子,但我认为这对其他人有用,而且这是 Google 上的第一个回复。
我对这种方法(子shell)和使用“等待”有疑问。但是,当我在函数内运行它时,我能够执行以下操作:
当我运行它时:
在我收到提示之前,有 5 秒的延迟。
Sorry for the response to an old post, but I figure this is useful to others, and it's the first response on Google.
I was having an issue with this method (subshells) and using 'wait'. However, as I was running it inside a function, I was able to do this:
And when I run it:
And there's a delay of 5 seconds before I get my prompt back.
subshell 解决方案有效,但我也希望能够等待后台作业(并且最后没有“完成”消息)。来自子 shell 的
$!
在当前交互式 shell 中不是“可等待的”。对我有用的唯一解决方案是使用我自己的等待函数,这非常简单:足够简单。
The subshell solution works, but I also wanted to be able to wait on the background jobs (and not have the "Done" message at the end).
$!
from a subshell is not "waitable" in the current interactive shell. The only solution that worked for me was to use my own wait function, which is very simple:Easy enough.