how to process several commands concurrently and continue the rest only after all is done on ubuntu

假装没事ソ 提交于 2020-01-17 06:19:11

问题


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.


回答1:


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

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.

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



回答2:


As you don't seem to be doing too well with fifo-based synchronisation, you could maybe try using GNU Parallel, which, when installed creates a symbolic link called sem that acts as a semaphore.

So, in Terminal 1, you would do:

sem -j 2 --id mark CommandA

and in Terminal 2, you would do:

sem -j 2 --id mark CommandB

Then, anywhere you wanted to wait for both to finish, you would do:

sem --id mark --wait ; CommandRest


来源:https://stackoverflow.com/questions/38694831/how-to-process-several-commands-concurrently-and-continue-the-rest-only-after-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!