我有一个 bash 脚本,它以串行方式调用相同的 perl 脚本。 bash 脚本用于收集总体结果,而 perl 脚本则收集给定属性的模拟结果。
bash 脚本如下所示:
mkdir ./results/csv && \
../perlscripts/v2csv.pl -v -F reach results/Heterogeneous*.vec > ./results/csv/reach.csv
../perlscripts/v2csv.pl -v -F roundTrip results/Heterogeneous*.vec > ./results/csv/RT.csv
../perlscripts/v2csv.pl -v -F downlink results/Heterogeneous*.vec > ./results/csv/DL.csv
../perlscripts/v2csv.pl -v -F clusters results/Heterogeneous*.vec > ./results/csv/clusters.csv
通过一次调用一个 perl 脚本来收集结果确实很长;我正在寻找一种方法,允许我在 bash 脚本中并行调用 perl 脚本的不同变体。有没有办法在 bash 中实现这一目标?
只是为了澄清,我不希望调用 perl 脚本的命令以任何方式相互依赖。我希望它们全部在同一时间点启动,就好像我有 4 个独立的 bash 终端,每个终端都执行这些命令之一。
相似的: https://stackoverflow.com/questions/15644991/running-several-scripts-in-parallel-bash-script
答案1
如果您已gnu parallel
安装,则可以仅使用命令制作脚本,例如:
../perlscripts/v2csv.pl -v -F reach results/Heterogeneous*.vec > ./results/csv/reach.csv
../perlscripts/v2csv.pl -v -F roundTrip results/Heterogeneous*.vec > ./results/csv/RT.csv
../perlscripts/v2csv.pl -v -F downlink results/Heterogeneous*.vec > ./results/csv/DL.csv
../perlscripts/v2csv.pl -v -F clusters results/Heterogeneous*.vec > ./results/csv/clusters.csv
然后并行运行它们:
mkdir ./results/csv && parallel :::: myscript.sh
或者,调用命令并使用{}
- 默认替换字符串:
mkdir ./results/csv && parallel ../perlscripts/v2csv.pl -v -F {} \
results/Heterogeneous*.vec '>' ./results/csv/{}.csv ::: reach roundTrip downlink clusters
将并行运行以下命令:
../perlscripts/v2csv.pl -v -F reach results/Heterogeneous*.vec > ./results/csv/reach.csv
../perlscripts/v2csv.pl -v -F roundTrip results/Heterogeneous*.vec > ./results/csv/roundTrip.csv
../perlscripts/v2csv.pl -v -F downlink results/Heterogeneous*.vec > ./results/csv/downlink.csv
../perlscripts/v2csv.pl -v -F clusters results/Heterogeneous*.vec > ./results/csv/clusters.csv
答案2
../_Cscripts/v2csv.pl -v -F reach results/Heterogeneous*.vec > ./results/csv/reach.csv &
../_Cscripts/v2csv.pl -v -F roundTrip results/Heterogeneous*.vec > ./results/csv/RT.csv &
../_Cscripts/v2csv.pl -v -F downlink results/Heterogeneous*.vec > ./results/csv/DL.csv &
../_Cscripts/v2csv.pl -v -F clusters results/Heterogeneous*.vec > ./results/csv/clusters.csv &
wait
& 将程序置于后台。如果你关心的话,等待他们停下来。
答案3
您可以尝试以下语法:
mkdir ./results/csv && (script0 & script1 &)
这将在后台运行脚本,而不是等待它们完成。括号引入了一个子 shell 组(这样,如果 mkdir 命令失败,则不会运行任何脚本)和&
请求后台执行(立即将控制权返回到外壳)。