Tuesday, February 20, 2018

How to run shell commands in parallel under for loop

I came across a situation where i need to run multiple commands at the same time i.e. in parallel.
I had to run the for loop to capture the device name and then run some write tests on all these devices at the same time.

 A "for" loop is completed with "do" , "done" semantic.
 # for disk in `lsscsi |grep 'SDIFC10-0720801'|awk '{print $6}' `; do fio --ioengine=libaio --direct=1 --name=test --filename=$disk --bs=4k --iodepth=10 --size=1000M --readwrite=write ; done

But this will end up running the fio command sequentially one after another.
The little secret is to use "&" instead of ";" after do phrase :
 # for disk in `lsscsi |grep 'SDIFC10-0720801'|awk '{print $6}' `; do fio --ioengine=libaio --direct=1 --name=test --filename=$disk --bs=4k --iodepth=10 --size=1000M --readwrite=write & done

And this will run the fio command on all the disks at the same time.