1

I have an text file that holds a list of jobs to process. It could process them all at once, but it overloads computer resources and some jobs fail. It could also process one line at a time, but the overall processing time is too long.

My customer is asking us to process 5 at a time to see if we can speed up processing time without missing jobs along the way. I feel like this is possible and likely pretty easy, but my brain is stuck on it and I can't figure it out.

Each job is on one row of the text file. Is there a way to load a list of jobs to run, but limit it to running 5 at a time?

Emile
  • 185
  • 1
  • 9

2 Answers2

1

this should execute in the manner you are looking for. I have not tested it though. It will work only on BASH.

IFS=$'\n' read -d '' -r -a jobs < list_of_jobs.txt
total_number_of_jobs=$( wc -l list_of_jobs.txt | awk '{print $1}' )
i=0;
accumulated=0;
limit=5; #How many to process at the same time
while [ $i -lt $total_number_of_jobs ]
do
    while [ $accumulated -lt $limit ] && [ $i -lt $total_number_oj_jobs ]
    do
        ./${jobs[$i]} &
        (( i ++ ))
        (( accumulated ++ ))
    done
    wait
    accumulated=0

done

Let me know if it helps you. Regards!

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49
1

I would recommend GNU Parallel as follows:

parallel -j 5 -a YourJobFile

There are lots of options, try adding —-eta or —-progress

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432