3

Let me start this by saying that I'm still learning bash scripting and I'm not aware of many things. If I miss something, feel free to scold me as you please.

Onward, I'm creating a simple bash script to run a ping to 2 different hosts and output to a file. I already got that covered, and it's working. Out of curiosity, I added in the script to output date after and before the ping output print, so I know how long it did take to run the pinging. I set ping to 1 second interval, 10 pings, 2 hosts. My first date (was a long time ago...) had 14:36:20 as hourstamp. The second date (after the ping) had 14:36:40 as hourstamp.

So, the date waited for both pings to finish. This means the pings ran sequentially.

Can I make them run concurrently, so I spend 10 seconds running the script instead of 20 seconds?

Here comes the snippet of my script:

date >> teste.txt
cat hosts.txt |  while read output
do   ping -c 10 -i 1 "$output" >> teste.txt
done
date >> teste.txt

There are several other things I will want this is script to do, but I promise I will only ask if I can't find a satisfactory answer on Barsa.

PS: hosts.txt is where I store the hosts I want to ping.

3

The wording of your question is a little difficult to understand, but I think you are looking for something like :

command & 
command &
wait

The script will execute the two commands in the background and wait for the response.

However, in your case, you need to be careful of a race condition. You probably would want to send the output of the individual commands into temporary files and then cat them into the main file, so you would probably do something like :

command > "${tmp1}" & 
command > "${tmp2}" & 
wait
cat "${tmp1}" "${tmp2}" >> "${main}"
rm -f "${tmp1}" "${tmp2}"

Update: You could try xargs (e.g. https://stackoverflow.com/questions/15755422/linux-bash-script-to-ping-multiple-hosts-simultaneously)

  • Hi sire! Thank you for your help! Adding & will make the two commands run "side by side" (that is, concurrently, beginning together and finishing together)? I thought & would just put the command on background. – Rafael Umbelino May 20 '16 at 17:59
  • Effectively yes. As you've discovered, normally a script processes line-by-line. However & means the script can send a long-running command to the background and proceed to the next line. "wait" then tells the script to "wait there and not process any further until the background tasks have completed". Hope that helps clarify ? – Little Code May 20 '16 at 18:01
  • I understood, but I believe didn't quite grasp it. Changed my script to match your example and it still took me 20s to run the entire script (which is just the 2 ten seconds ping. – Rafael Umbelino May 20 '16 at 18:06
  • Hmm.... have updated the answer with an alternative based on xargs. – Little Code May 20 '16 at 18:10
  • And you can do that using functions: function bla() { does blabla } ; bla& etc. – Luciano Andress Martini May 20 '16 at 18:10
0

With GNU Parallel you can do this:

date >> teste.txt
cat hosts.txt |  parallel -j0 ping -c 10 -i 1 {} >> teste.txt
date >> teste.txt

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.