在初始化/bash 脚本中同时执行多个程序

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

Execute several programs at the same time in an initialisation/bash script

linuxbashscriptingrcs

提问by Eduardo

Hello I am working with a simulator that uses rcS scripts to boot, this is my script

你好,我正在使用一个使用 rcS 脚本启动的模拟器,这是我的脚本

cd /tests
./test1 &
./test2 &
./test3 &
./test4 
exit

What I want is run all the test at the same time and that the exit command is executed only when all the previous test have finished. And not only when test 4 has finished, is this possible?. Thank you.

我想要的是同时运行所有测试,并且只有在所有先前的测试都完成后才会执行 exit 命令。不仅当测试 4 完成时,这可能吗?。谢谢你。

回答by gak

You can use wait:

您可以使用等待:

./test1 &
./test2 &
./test3 &
./test4 &
wait

From the bash man page:

从 bash 手册页:

wait [n ...] Wait for each specified process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job's pipeline are waited for. If n is not given, all currently active child processes are waited for, and the return status is zero. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.

wait [n ...] 等待每个指定的进程并返回其终止状态。每个 n 可能是进程 ID 或作业规范;如果给出了作业规范,则等待该作业管道中的所有进程。如果未给出 n,则等待所有当前活动的子进程,返回状态为零。如果 n 指定一个不存在的进程或作业,则返回状态为 127。否则,返回状态为最后等待的进程或作业的退出状态。

回答by firejox

xargscan support parallel

xargs可以支持并行

So just like this:

所以就像这样:

seq 4|xargs -i -n 1 -P 4 ./test{} 

回答by Eduardo

Something along the lines of

类似的东西

cd /tests
./test1 &
./test2 &
./test3 &
./test4 &
wait
exit

(I am assuming bash shell)

(我假设 bash shell)