Heyas,
Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something
The last point for motivation was How to run scripts parallely inside shell script? - Page 2
Where I've posted a screen-shot from my very first (and small) approach.
Now i want to write a script that will work for/with:
- 'any' amount of scripts passed,
- provides a limiter, so only X scripts are running simultaneously
- Prints the status code of each of the scripts ran
So I had it running and working, but then figured, I've only tested 4 scripts (sleep [5-30secs), but the LIMIT was 5, so I reduced the limit to 2, now i cant figure why its not working.
Actually, it wont even run (properly) with the default LIMIT of 5 even when only 4 scripts are passed
And the previous code no longer works neither, since I've renamed some of the variables, for a better 'sort'.
My approach is:
- scripts_remains = array of arguments (the scripts passed)
- scripts_start = array is filled only temporary, as long the counter scripts_running is less than the LIMIT.
- While filling the scripts_start, i remove the according array element from scripts_remains.
- scripts_todo = array that gets filled as soon the scripts_start entry is started. (and afterwards removed from scripts_start)
- scripts_id = array which holds the PID of the script of the same ID of scripts_todo
When the script no longer finds the PID, it moves the element from scripts_todo/scripts_id to done_scripts, and fills done_ret, both with the same array id.
(this part works still/again)
But the starting array gets only the first script and not more... however, it does list as many entries as the LIMIT allows, but all contain the first script and only 1 entry does actually show the proper PID.
scripts_remains still holds all array elements, which confuses me, furthermore scripts_todo holds only 1 array element. but lists as many entries as the LIMIT allows.
NOTE: This script is(will be) part of my TUI (text user interface) package, therefore all those tui-* commands ARE available and 'valid' - on my system at least.
Please see the screen-shot on how it outputs, and the code segment that is faulty.
Acknowledge, that the error referring to line 332 is due to the wrong set / missing array values.
I have a small script, that is working, but since i want/need to have some handlers and argument handling around it,
it does in no way refer the issues i experience.
I'm asking if you either see a 'wrong'-code, in the part visible, or if you would have an advice for another approach/method to achieve my goal?
Thank you in advance
EDIT:
It seems that the first script (only one loaded to array) is started/executed as many times as the limit is set to, without continuing to the next array element..
But i cant pinpoint it.
If you need the full code, i'd happily share it, just since it wont run (unless you have TUI installed) i thought its not really helpfull anyway.
EDIT:
Done the 'no limit' part, which works:
while [[ ! -z "${scripts_remains[@]}" ]]
do # Start the script:
script="${scripts_remains[$counter_start]}"
scripts_todo[$counter_start]="$script"
# Generate the command
[[ [./] = "${script:0:1}" ]] && PRE="" || PRE="./"
cmd="${PRE}\"$script\" ; echo \$? > \"$TEMP/$script.ret\""
#doLog "Executing: $cmd"
# Get PID to array
( eval $cmd ) &
scripts_id[$counter_start]="$!"
# Remove it from array, and move to next entry
unset scripts_remains[$counter_start]
((counter_start++))
done
So I need to get this code segment working with a LIMIT factor, so only LIMIT amount of scripts are started as long scripts_running is smaller than the LIMIT - see the main post -
Screen-shot *1small : the non-working part
Screen-shot *2small : the working no-limit part
EDIT3:
I'm now 100% sure, I've had somewhere screwed up with the arrays, but atm I'm too blind...