如何在ubuntu上完成所有命令并发处理并继续其余命令 [英] how to process several commands concurrently and continue the rest only after all is done on ubuntu
问题描述
具体来说,我需要在Ubuntu 上的shell中同时运行两个命令,例如 command_A 和 command_B 。我还有一些其他命令仅在 command_A 和 command_B 完成后才需要运行,这些命令称为 command_rest 。此外, command_A 和 command_B 在单独的终端中运行,完成后便可以自行关闭。我猜这可能需要与信号和 wait 和 gnome-terminal 相关的技术,但我找不到解决方案。
To be specific, I have two command need to be run in shell on Ubuntu at the same time, like command_A and command_B. And I have some other commands need to be run only after command_A and command_B has finished, named as command_rest. In addition, command_A and command_B run in separate terminals and when they are finished they can close themselves. This maybe need techniques related to signal and wait and gnome-terminal i guess, but i cannot find a solution.
推荐答案
您可以使用fifo进行以下操作:
You can do it like this using a fifo to synchronise:
# Once in either Terminal
mkfifo A B
# In first Terminal
( echo CommandA; sleep 3; echo done > A ) &
# In second Terminal
( echo CommandB; sleep 8; echo done > B ) &
# In third Terminal
read < A; read < B; echo Rest
基本上,在 Rest
运行之前,它必须同时从 A
和 B
中读取内容,直到 CommandA
已完成并写入 A
,并且 CommandB
已完成并写入到 B
。
Basically, before Rest
can run, it has to have read something from both A
and B
and nothing will arrive for it to read until CommandA
has finished and written to A
and also CommandB
has finished and written to B
.
上面只是一个回显 CommandA $ c的示例$ c>和
CommandB
和 Rest
而不是运行我没有的命令。您实际上会想要这样的东西(我已经对其进行了修改,因此您可以在各个终端中的不同目录中)
The above is just an example that echoes CommandA
and CommandB
and Rest
instead of running commands that I don't have. You will actually want something like this (I have modified it so you can be in different directories in the various Terminals)
# Once in either Terminal
mkfifo /tmp/A /tmp/B
# In first Terminal
( CommandA; echo done > /tmp/A ) &
# In second Terminal
( CommandB; echo done > /tmp/B ) &
# In third Terminal
read < /tmp/A; read < /tmp/B; commandRest
这篇关于如何在ubuntu上完成所有命令并发处理并继续其余命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!