在并行 bash 脚本中运行多个脚本 [英] Running several scripts in parallel bash script

查看:39
本文介绍了在并行 bash 脚本中运行多个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 bash 脚本,其中包含串行运行的其他脚本.但是,运行它们需要相当长的时间.有没有办法并行运行这些脚本来提高整体性能?它们彼此独立.

I have a bash script that contains other scripts inside that are run in series. However, it takes a decent amount of time to run them all. Is there a way to run these scripts in parallel to improve overall perfomance? They are independent of each other.

它看起来类似于:

#!/bin/bash

#some code here
cppcheck.sh
churn.sh
run.sh

<小时>

更新:

**git log --pretty=format: --numstat | perl -ane'$c{$F[2]} += abs($F[0]+$F[1]) 
if $F[2];END {print "$_	$c{$_}
" for sort keys %c}' > ${OUTPUT_DIR}/churn.txt**
sed -i -e '/deps/d;/build/d;/translations/d;/tests/d' -e 30q ${OUTPUT_DIR}/churn.txt
sort -r -n -t$'	' -k2 ${OUTPUT_DIR}/churn.txt -o ${OUTPUT_DIR}/churn.txt
echo "set term canvas size 1200, 800; set output '${OUTPUT_DIR}/output.html'; 
unset key; set bmargin at screen 0.4; set xtics rotate by -90 scale 0,0; 
set ylabel 'Number of lines changed (total)'; set title 'Files with high churn 
level';set boxwidth 0.7; set style fill solid; set noborder; 
plot '${OUTPUT_DIR}/churn.txt' using 2:xticlabels(1) with boxes" | gnuplot
echo "finished running churn.sh!"

这是churn.sh里面的代码.粗体命令需要 40 秒左右的时间来实现.如果在主脚本中,我在 churn.sh & 之后放置了&符号,则会引发错误,指出 sed 无法读取 churn.txt 文件(因为它尚未创建).似乎它不会等到输出保存在文件中.我在该命令之后插入了等待,但没有帮助.

This is the code inside churn.sh. The bold command takes 40 or so secs to implement. If in a main script I put ampersand after churn.sh &, it throws an error saying sed can't read churn.txt file (since it's not created yet). It seems that it doesn't wait till the output is saved in a file. I inserted wait after that command but it doesn't help.

推荐答案

使用 & 在后台运行它可以解决问题

Using the & to run it in the background will do the trick

cppcheck.sh &
churn.sh &
run.sh &

wait
echo "All 3 complete"

它会为每个人分出一个新进程.

It will fork off a new process for each of them.

bash wait 也将派上用场,如注释,如果您有要在父脚本上运行的内容,这三个完成之后.

The bash wait will also come in handy as stated in the comments, if you have something to be run on the parent script, after these three finish.

如果没有参数,它将等待所有子进程完成,然后继续执行父脚本.

Without an argument it will wait for all child processes to complete, and then resume execution of the parent script.

您面临的问题似乎与此直接相关.变量集仅对 定义它们的子 shell 可见.因此,如果您在父脚本中指定了 OUT_DIR,则在它分叉时子脚本将看不到它.在这种情况下,正确的做法是将变量export 作为环境变量.

The issues you are facing seem to be directly related to this. Variables set are only visible to the sub-shell in which they are defined. So, if you have OUT_DIR specified in the parent script, it won't be visible to the child script when it forks off. The right thing to do in this case would be to export the variable as an environment variable.

这篇关于在并行 bash 脚本中运行多个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆