Running multiple script s in parallel bash script

I have a bash script that contains other scripts inside that run sequentially. However, it takes quite a while to launch them. Is there a way to run these scripts in parallel to improve overall performance? They are independent of each other.

He looks like:

#!/bin/bash #some code here cppcheck.sh churn.sh run.sh 



Update:

 **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!" 

This is the code inside churn.sh. The bold command takes about 40 seconds to complete. If in the main script I put an ampersand after churn.sh &, it gives an error message when sed cannot read the churn.txt file (since it has not been created yet). It seems that it does not wait until the output is saved in a file. I inserted wait after this command, but that does not help.

+45
bash parallel-processing
Mar 26 '13 at 18:44
source share
1 answer

Using & to run in the background will do the trick

 cppcheck.sh & churn.sh & run.sh & wait echo "All 3 complete" 

A new process will open for each of them.

bash wait will also be useful, as pointed out in the comments, if you have something to run on the parent script after these three finishes.

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




The problems you are facing seem to be directly related to this. A set of variables is displayed only under the <shell in which they are defined . So, if you have OUT_DIR specified in the parent script, it will not be visible to the script child when it is disabled. The right thing in this case would be an export variable as an environment variable.

+86
Mar 26 '13 at 18:45
source share



All Articles