How to use parallel execution in a shell script?
Asked Answered
B

7

21

I have a C shell script that does something like this:

#!/bin/csh
gcc example.c -o ex
gcc combine.c -o combine
ex file1 r1     <-- 1
ex file2 r2     <-- 2
ex file3 r3     <-- 3
#... many more like the above
combine r1 r2 r3 final
\rm r1 r2 r3

Is there some way I can make lines 1, 2 and 3 run in parallel instead of one after the another?

Bleary answered 7/5, 2010 at 19:9 Comment(0)
T
13

Convert this into a Makefile with proper dependencies. Then you can use make -j to have Make run everything possible in parallel.

Note that all the indents in a Makefile must be TABs. TAB shows Make where the commands to run are.

Also note that this Makefile is now using GNU Make extensions (the wildcard and subst functions).

It might look like this:

export PATH := .:${PATH}

FILES=$(wildcard file*)
RFILES=$(subst file,r,${FILES})

final: combine ${RFILES}
    combine ${RFILES} final
    rm ${RFILES}

ex: example.c

combine: combine.c

r%: file% ex
    ex $< $@
Therapy answered 7/5, 2010 at 19:13 Comment(4)
Your -l combine should be -o combineExaggerated
Would you put the final rule first to make it the default?Palanquin
I like your answer but I just realized his question states more than just 3 possible files and this doesn't seem to scale all that well.Exaggerated
Beware the habit of running "make -j" without the integer argument. It will keep spawning as fast as it can. This can cripple a machine during a build with a lot of source files. A better habit is something like "make -j8"Secondary
T
12

In bash I would do;

ex file1 r1  &
ex file2 r2  &
ex file3 r3  &
wait
... continue with script...

and spawn them out to run in parallel. You can check out this SO thread for another example.

Tautog answered 7/5, 2010 at 19:24 Comment(0)
E
5
#!/bin/bash

gcc example.c -o ex
gcc combine.c -o combine

# Call 'ex' 3 times in "parallel"
for i in {1..3}; do
  ex file${i} r${i} &
done

#Wait for all background processes to finish
wait

# Combine & remove
combine r1 r2 r3 final
rm r1 r2 r3

I slightly altered the code to use brace expansion {1..3} rather than hard code the numbers since I just realized you said there are many more files than just 3. Brace expansion makes scaling to larger numbers trivial by replacing the '3' inside the braces to whatever number you need.

Exaggerated answered 7/5, 2010 at 19:27 Comment(0)
P
3

you can use cmd & and wait after

#!/bin/csh
echo start
sleep 1 &
sleep 1 &
sleep 1 &
wait
echo ok

test:

$ time ./csh.sh 
start
[1] 11535
[2] 11536
[3] 11537
[3]    Done                   sleep 1
[2]  - Done                   sleep 1
[1]  + Done                   sleep 1
ok

real    0m1.008s
user    0m0.004s
sys 0m0.008s
Pliers answered 7/5, 2010 at 19:32 Comment(0)
L
1

GNU Parallel would make it pretty like:

seq 1 3 | parallel ex file{} r{}

Depending on how 'ex' and 'combine' work you can even do:

seq 1 3 | parallel ex file{} | combine

Learn more about GNU Parallel by watching http://www.youtube.com/watch?v=LlXDtd_pRaY

Longobard answered 11/6, 2010 at 0:41 Comment(0)
P
0

You could use nohup ex :

nohup ex file1 r1 &    
nohup ex file2 r2 &
nohup ex file3 r3 &
Polydeuces answered 12/2, 2011 at 1:9 Comment(1)
nohup is massive overkill here. Its goal is to let a process continue running even if the terminal the shell is running in is closed, which isn't what the OP is asking for here. Moreover, you don't need it even for that -- the shell's builtin disown command can do the parts of that that can't be done just by redirecting stdin/stdout/stderr away from the TTY. And further, nohup prevents the shell from finding out if any of the ex commands failed after-the-fact, which the OP here presumably does want.Gurglet
W
0

xargs can do it:

seq 1 3 | xargs -n 1 -P 0 -I % ex file% r%

-n 1 is for "one line per input", -P is for "run each line in parallel"

Wershba answered 23/3, 2015 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.