1

I've tried to handle this problem, I've a little script to connect into a list of host stored in a file.

#!/bin/bash
num=$(wc -l < ip.txt)
while read ip; do
    mysql --login-path=login -h $ip "database" < "query.sql"
    echo "Script 1 - Remaining: "$num $ip
    num=$((num-1))
done < ip.txt
echo "Script 1 Finished!!"

I want to run multiple MySQL queries at once for example 10 or 20 so I can finish my test faster, I've tried to make another shell script who runs 4 scripts at once but I really don't know how to handle it.

4

Put the mysql jobs in the background (by terminating the command with &):

#!/bin/bash
num=$(wc -l < ip.txt)
while read ip; do
    mysql --login-path=login -h $ip "database" < "query.sql" &
    num=$((num-1))
done < ip.txt
wait
echo "Script 1 Finished!!"

The wait (a shell builtin command) causes the script to wait until all background jobs have completed.

This is a pretty simple-minded way of doing this, you might want to add logic to put e.g. at most 4 jobs in the background at once.

An alternative is to use parallel which works a bit like xargs but is designed to run jobs in parallel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.