1

I am sending processes to a server, but I only want a certain number running at a time. Thus I loop which checks how many jobs are running and only submits new one if the current amount of jobs do not exceed some measure. To this end I need to wait if the current queue is full, to submit more jobs.

I am wondering how I would achieve this via something like:

#!/bin/bash

for i in `seq 10`; do
    if [ condition1 -le condition2 ]; do
        #>>> Submit jobs
    else
        # SERVER IS FULL
        #
        # Wait 10min and try again, by returning to start of the loop
        # with the current value of $i intact
        sleep 10m 
done

If my intuition is correct the above loop will try the next time with $(($i + 1)) if the server is full at step $i - thus leaving some jobs unprocessed.

Any suggestions for how this can be dealt with? I.e. how can I break out of the second clause (else) and return to the start of the loop.

1
  • Instead of if condition do process else wait, try while not condition wait ; do process
    – Mathieu
    Jan 22, 2016 at 16:15

1 Answer 1

3

A simple solution is to change your logic slightly:

i=1;
while [ "$i" -le 10 ]; do
    if [ condition1 -le condition2 ]; do
        #>>> Submit jobs

        ## Increment the value of $i if a job was submitted
        i=$((i+1))
    else
        # SERVER IS FULL
        #
        # Wait 10min and try again, by returning to start of the loop
        # with the current value of $i intact
        sleep 10m 
done

This way, $i will only be incremented when a job was submitted. If the server is full, the script will wait 10 minutes and repeat with the same value of $i.

2
  • Waiting now :) - could one potentially use retry and until as well in cases like this?
    – Astrid
    Jan 22, 2016 at 16:22
  • 1
    @Astrid bash has no retry as far as I know but yes, you can do until [ "$i" = 11 ] for example.
    – terdon
    Jan 22, 2016 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .