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

查看:726
本文介绍了并行运行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 "$_\t$c{$_}\n" 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$'\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的code。大胆的命令需要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的 也能派上用场的规定在评论中,如果你有什么要对父脚本运行,这三个完成。

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 ,也不会当它叉掉是孩子脚本可见。在这种情况下,做正确的事情将是导出变量作为环境变量。

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天全站免登陆