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.
The command to do this is CTRL-B :setw synchronize-panes on. Now you can run scriptName $fruit and view the output on all panes at the same time.
The bg and fg Commands. A handy Linux command for running two scripts simultaneously would be the bg command. Using the bg command, we can resume and control how jobs are run in our terminal. We can set a script to run in the background while running a new script in the foreground.
If your machine has at least two CPU threads, you will be able to max-out CPU resources using multi-threaded scripting in Bash. The reason for this is simple; as soon as a secondary 'thread' (read: subshell) is started, then that subsequent thread can (and often will) use a different CPU thread.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With