user3164754
user3164754

Reputation: 215

parallel iterations in for loop in shell

I am trying to run a command across some lines in a text file. I am using for loop for this as below.

for LINE in `cat $LogDir/details.lst`
do
#Queue state dump
#The following amqldmpa command dumps the queue state every 10 seconds for one minute.
amqldmpa -m $QMGR -c A -u 1 -q $LINE -d 3 -n 6 -s 10 -f $LogDir/$LINE.txt
done

Now there are close to 20 lines in the details.lst file. How can i run this command against all these lines at the same time in parallel? Also how can i get out of the for loop only after all the lines have been successfully executed in the command?

Please help.

Upvotes: 0

Views: 86

Answers (1)

glenn jackman
glenn jackman

Reputation: 247052

  • that's the wrong way to iterate over the lines of a file. Use this a while loop.
  • put each amqldmpa in the background
  • assuming your shell is bash, use the wait builtin
while IFS= read -r line; do
    amqldmpa --options... &
done < "$LogDir/details.lst"
wait
echo "all jobs done"

You do lose the exit statuses of all the backgrounded jobs that way though.

Upvotes: 2

Related Questions