Running several scripts in parallel bash script

BashParallel Processing

Bash Problem Overview


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.

It looks similar to:

#!/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 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.

Bash Solutions


Solution 1 - Bash

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.

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.


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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBdarView Question on Stackoverflow
Solution 1 - BashAnirudh RamanathanView Answer on Stackoverflow