Run Concurrent Instances of a Command Inside a Shell Script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2














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.










share|improve this question




























    2














    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.










    share|improve this question


























      2












      2








      2







      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.










      share|improve this question















      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.







      shell-script parallelism






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 20 '16 at 17:58









      meuh

      31.5k11854




      31.5k11854










      asked May 20 '16 at 17:46









      Rafael Umbelino

      162




      162




















          2 Answers
          2






          active

          oldest

          votes


















          2














          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)






          share|improve this answer






















          • 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





          share|improve this answer




















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f284510%2frun-concurrent-instances-of-a-command-inside-a-shell-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            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)






            share|improve this answer






















            • 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
















            2














            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)






            share|improve this answer






















            • 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














            2












            2








            2






            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)






            share|improve this answer














            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)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:39









            Community

            1




            1










            answered May 20 '16 at 17:55









            Little Code

            1928




            1928











            • 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

















            • 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
















            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




            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




            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




            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




            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





            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





            share|improve this answer

























              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





              share|improve this answer























                0












                0








                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





                share|improve this answer












                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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 18 at 22:06









                Ole Tange

                12k1451105




                12k1451105



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f284510%2frun-concurrent-instances-of-a-command-inside-a-shell-script%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown






                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay