user1301593
user1301593

Reputation: 631

How to run parallel for loops

I'm not very familiar with bash, but I would like split up this code such that I can run it on a server with 12 processors:

#!/bin/bash
#bashScript.sh
for i in {1..209}
do

Rscript Compute.R $i

done

How would I go about achieving this?

Thanks!

Upvotes: 4

Views: 187

Answers (3)

Ole Tange
Ole Tange

Reputation: 33748

Use GNU Parallel:

parallel Rscript Compute.R ::: {1..209}

10 seconds installation:

wget -O - pi.dk/3 | sh

Watch the intro video for a quick introduction: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Upvotes: 1

nosid
nosid

Reputation: 50134

Use xargs with the option --max-procs (-P). If there are enough arguments, xargs will use exactly this number of concurrent processes to process the input:

#! /bin/bash
seq 209 |
xargs -P12 -r -n1 Rscript Compute.R

Upvotes: 4

Muc
Muc

Reputation: 1526

Try:

#!/bin/bash
#bashScript.sh
for i in {1..209}
do

Rscript Compute.R $i &

done

Upvotes: 1

Related Questions