@@ -184,59 +184,38 @@ stop_start_system() {
184184stop_start_router () {
185185 prepare_ids_clause
186186 router=(` mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e " select uuid from vm_instance where state=\" Running\" and type=\" DomainRouter\" $zone$vmidsclause " ` )
187- length_router=( ${# router[@]} )
187+ length_router=${# router[@]}
188188
189189 echo -e " \nStopping and starting $length_router running routing vm(s)$inzone$withids ... "
190190 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] Stopping and starting $length_router running routing vm(s)$inzone$withids ... " >> $LOGFILE
191191
192192 # Spawn reboot router in parallel - run commands in <n> chunks - number of threads is configurable
193193
194+ if [ $maxthreads -gt $length_router ]; then
195+ maxthreads=$length_router
196+ fi
194197 pids=()
195198 for d in " ${router[@]} " ; do
196199
197200 reboot_router $d &
198201
199202 pids=( " ${pids[@]} " $! )
200-
201- length_pids=(${# pids[@]} )
202- unfinishedPids=(${# pids[@]} )
203-
204- if [ $maxthreads -gt $length_router ]; then
205- maxthreads=$length_router
206- fi
203+ length_pids=${# pids[@]}
207204
208205 if [ $length_pids -ge $maxthreads ]; then
209- while [ $unfinishedPids -gt 0 ]; do
210- sleep 10
211- count=0
212- for (( i = 0 ; i < $length_pids ; i++ )) ; do
213- if ! ps ax | grep -v grep | grep ${pids[$i]} > /dev/null; then
214- count=` expr $count + 1`
215- fi
216- done
217-
218- if [ $count -eq $unfinishedPids ]; then
219- unfinishedPids=0
220- fi
221-
222- done
223-
224- # remove all elements from pids
225- if [ $unfinishedPids -eq 0 ]; then
226- pids=()
227- length_pids=(${# pids[@]} )
228- fi
229-
206+ # Wait for $maxthreads number of processes to finish
207+ wait
208+ # Clear the pids array for the next batch
209+ pids=()
230210 fi
231211
232212 done
233213
234214 if [ " $length_router " == " 0" ]; then
235215 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] No running router vms found \n" >> $LOGFILE
236216 else
237- while [ $unfinishedPids -gt 0 ]; do
238- sleep 10
239- done
217+ # Wait for the remaining background processes to finish
218+ wait
240219
241220 echo -e " Done restarting router(s)$inzone$withids . \n"
242221 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] Done restarting router(s)$inzone$withids . \n" >> $LOGFILE
@@ -288,49 +267,29 @@ reboot_router(){
288267restart_networks (){
289268 networks=(` mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e " select n.id
290269 from networks n, network_offerings no where n.network_offering_id = no.id and no.system_only = 0 and n.removed is null$zone " ` )
291- length_networks=( ${# networks[@]} )
270+ length_networks=${# networks[@]}
292271
293272 echo -e " \nRestarting $length_networks networks$inzone ... "
294273 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] Restarting $length_networks networks$inzone ... " >> $LOGFILE
295274
296275 # Spawn restart network in parallel - run commands in <n> chunks - number of threads is configurable
297276
277+ if [ $maxthreads -gt $length_networks ]; then
278+ maxthreads=$length_networks
279+ fi
298280 pids=()
299281 for d in " ${networks[@]} " ; do
300282
301283 restart_network $d &
302284
303285 pids=( " ${pids[@]} " $! )
304-
305- length_pids=(${# pids[@]} )
306- unfinishedPids=(${# pids[@]} )
307-
308- if [ $maxthreads -gt $length_networks ]; then
309- maxthreads=$length_networks
310- fi
286+ length_pids=${# pids[@]}
311287
312288 if [ $length_pids -ge $maxthreads ]; then
313- while [ $unfinishedPids -gt 0 ]; do
314- sleep 10
315- count=0
316- for (( i = 0 ; i < $length_pids ; i++ )) ; do
317- if ! ps ax | grep -v grep | grep ${pids[$i]} > /dev/null; then
318- count=` expr $count + 1`
319- fi
320- done
321-
322- if [ $count -eq $unfinishedPids ]; then
323- unfinishedPids=0
324- fi
325-
326- done
327-
328- # remove all elements from pids
329- if [ $unfinishedPids -eq 0 ]; then
330- pids=()
331- length_pids=(${# pids[@]} )
332- fi
333-
289+ # Wait for $maxthreads number of processes to finish
290+ wait
291+ # Clear the pids array for the next batch
292+ pids=()
334293 fi
335294
336295 done
@@ -339,9 +298,8 @@ restart_networks(){
339298 if [ " $length_networks " == " 0" ]; then
340299 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] No networks found \n" >> $LOGFILE
341300 else
342- while [ $unfinishedPids -gt 0 ]; do
343- sleep 10
344- done
301+ # Wait for the remaining background processes to finish
302+ wait
345303
346304 echo -e " Done restarting networks$inzone . \n"
347305 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] Done restarting networks$inzone . \n" >> $LOGFILE
@@ -392,49 +350,29 @@ restart_vpc(){
392350
393351restart_vpcs (){
394352 vpcs=(` mysql -h $db --user=$user --password=$password --skip-column-names -U cloud -e " select uuid from vpc WHERE removed is null$zone " ` )
395- length_vpcs=( ${# vpcs[@]} )
353+ length_vpcs=${# vpcs[@]}
396354
397355 echo -e " \nRestarting $length_vpcs vpcs... "
398356 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] Restarting $length_vpcs vpcs... " >> $LOGFILE
399357
400358 # Spawn restart vpcs in parallel - run commands in <n> chunks - number of threads is configurable
401359
360+ if [ $maxthreads -gt $length_vpcs ]; then
361+ maxthreads=$length_vpcs
362+ fi
402363 pids=()
403364 for d in " ${vpcs[@]} " ; do
404365
405366 restart_vpc $d &
406367
407368 pids=( " ${pids[@]} " $! )
408-
409- length_pids=(${# pids[@]} )
410- unfinishedPids=(${# pids[@]} )
411-
412- if [ $maxthreads -gt $length_vpcs ]; then
413- maxthreads=$length_vpcs
414- fi
369+ length_pids=${# pids[@]}
415370
416371 if [ $length_pids -ge $maxthreads ]; then
417- while [ $unfinishedPids -gt 0 ]; do
418- sleep 10
419- count=0
420- for (( i = 0 ; i < $length_pids ; i++ )) ; do
421- if ! ps ax | grep -v grep | grep ${pids[$i]} > /dev/null; then
422- count=` expr $count + 1`
423- fi
424- done
425-
426- if [ $count -eq $unfinishedPids ]; then
427- unfinishedPids=0
428- fi
429-
430- done
431-
432- # remove all elements from pids
433- if [ $unfinishedPids -eq 0 ]; then
434- pids=()
435- length_pids=(${# pids[@]} )
436- fi
437-
372+ # Wait for $maxthreads number of processes to finish
373+ wait
374+ # Clear the pids array for the next batch
375+ pids=()
438376 fi
439377
440378 done
@@ -443,9 +381,8 @@ restart_vpcs(){
443381 if [ " $length_vpcs " == " 0" ]; then
444382 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] No vpcs found \n" >> $LOGFILE
445383 else
446- while [ $unfinishedPids -gt 0 ]; do
447- sleep 10
448- done
384+ # Wait for the remaining background processes to finish
385+ wait
449386
450387 echo -e " Done restarting vpcs$inzone . \n"
451388 echo -e " [$( date " +%Y.%m.%d-%H.%M.%S" ) ] Done restarting vpcs$inzone . \n" >> $LOGFILE
0 commit comments