Search code examples
c++linuxbashparallel-processinggnu-parallel

How to use gnu_parallel to run multiple executable and/or bash scripts?


I've been recently attempting to run my scripts in parallel in a more convenient way than to open a several instances of terminal and executing in scripts separately.

I've been trying to learn how to use gnu_parallel for the past couple of days and I am still a bit clueless, and hoping if someone can provide a direct example.

Suppose I have a g++ compiled code called blah.exe and a bash script called blah.sh that will run alone perfectly fine, but I want to execute them in different directories.

I've been reading https://www.gnu.org/software/parallel/man.html#EXAMPLE:-Working-as-xargs--n1.-Argument-appending

and

https://www.biostars.org/p/182136/

but I am not totally clear about the syntax

To run these in series, I would do:

for i in 1 2 3 4
mv ./blah.exe directory$i
cd directory$i
./blah.exe all
cd ..
end

similarly

for i in 1 2 3 4
mv ./blah.sh directory$i
cd directory$i
source ./blah.sh all
cd ..
end

I am trying to under stand how I would split this load to 4 logical-threads in one command using parallel.

Could someone provide an example for this?

Thank you for your time.


Solution

  • Something like:

    parallel --dry-run 'cd directory{}; ../blah.exe all; source ../blah.sh all' ::: {1..4}
    

    No need to copy/move the executable, just run the same one.

    No need to cd .. afterwards, as it's a new process each time.

    Note this is not multi-threading, it is multi-processing.


    If you want to process discontiguous directory numbers, you can use:

    parallel ... ::: {1..4} 6 7 {11..14}
    

    If you want to process all directories, you can use:

    printf "%s\0" */ | parallel -0 'cd {}; pwd' 
    

    If you want to process all directories starting with FRED, you can use:

    printf "%s\0" FRED*/ | parallel -0 'cd {}; pwd'