Running bash scripts parallely

I have multiple bash script files named as run0.sh, run1.sh, run2.sh…runN.sh; which I want to run in parallel; instead of running each of them one-by-one. Also, Does assigning them into the no. of cores speed up the computation? How should I do that using the terminal?

Thanks & Regards,
Ankur

getconf _NPROCESSORS_ONLN: 8
ROOT Version: 6.24/02
Platform: Focal Release 20.04
Compiler: C++


You can run a process in the backgroud by adding an “&” at the end (e.g. ./run0.sh &) and starting them one after the other (./run0.sh &; ./run1.sh &; ./run2.sh &; ... etc), but for me, I’d run each in a separate terminal window, to better see any output from each one.
You can even do a script to open a terminal for each script. On XFCE, for instance:

#!/bin/bash
xfce4-terminal --hold -e "./run0.sh"
xfce4-terminal --hold -e "./run1.sh"
# and so on

I would leave a couple of threads (1 core?) free, though, just to keep some resources free :slight_smile: so, if you have 8 cores (16 threads?), I’d run max 14 scripts at the same time; but it also depends on the available RAM and the speed of your storage (especially if you are reading/writing a lot, the storage may not keep up with the processor)…

2 Likes

Thank You. :smiley: The reply is helpful. @dastudillo

A suggestion: GNU Parallel - GNU Project - Free Software Foundation

They have detailed tutorial and examples in their website: GNU Parallel Tutorial — GNU Parallel 20220822 documentation

Just take a few minute and you will know how use this best tool to help you to do the job

parallel -j 10 "./{}" ::: run*.sh
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.