Based on This solution I am trying to develop a script that would limit the number of processes running to 4. However as an alternative I want the jobs to be stored in an array that I reference by index. I have written the following:
todo_array[1]="echo start1;sleep 3;echo done1"
todo_array[2]="echo start2;sleep 3;echo done2"
todo_array[3]="echo start3;sleep 3;echo done3"
todo_array[4]="echo start4;sleep 3;echo done4"
todo_array[5]="echo start5;sleep 3;echo done5"
todo_array[6]="echo start6;sleep 3;echo done6"
todo_array[7]="echo start7;sleep 3;echo done7"
todo_array[8]="echo start8;sleep 3;echo done8"
todo_array[9]="echo start9;sleep 3;echo done9"
max_jobs=4
seq ${#todo_array[@]} | xargs -i --max-procs=$max_jobs bash -c $todo_array[{}]
however when I execute I am getting an empty 9 newlinex output.
What am I doing wrong?
Thanks
EDIT: I modified it to
seq ${#todo_array[@]} | xargs -i --max-procs=$max_jobs bash -c "$todo_array[{}]"
and I am getting the following weird output:
start1
start1
start1
start1
done1[2]
done1[3]
done1[1]
done1[4]
start1
start1
start1
start1
done1[5]
done1[6]
done1[7]
done1[8]
start1
done1[9]
Try this one: