how to run multiple shell scripts in parallel The Next CEO of Stack OverflowHow to determine...

Solving Integral Equation by Converting to Differential Equations

Unreliable Magic - Is it worth it?

Bold, vivid family

calculus parametric curve length

Preparing Indesign booklet with .psd graphics for print

Is it possible to search for a directory/file combination?

Inappropriate reference requests from Journal reviewers

In excess I'm lethal

Are there any unintended negative consequences to allowing PCs to gain multiple levels at once in a short milestone-XP game?

Why does the UK parliament need a vote on the political declaration?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

How do we know the LHC results are robust?

Limits on contract work without pre-agreed price/contract (UK)

Won the lottery - how do I keep the money?

What's the best way to handle refactoring a big file?

What flight has the highest ratio of time difference to flight time?

WOW air has ceased operation, can I get my tickets refunded?

What connection does MS Office have to Netscape Navigator?

Non-deterministic sum of floats

Would a galaxy be visible from outside, but nearby?

Return the Closest Prime Number

How did people program for Consoles with multiple CPUs?

Why has the US not been more assertive in confronting Russia in recent years?

Do I need to enable Dev Hub in my PROD Org?



how to run multiple shell scripts in parallel



The Next CEO of Stack OverflowHow to determine if a bash variable is empty?how to export VARs from a subshell to a parent shell?How can I sort du -h output by sizeview multiple log file in a single terminalHow to run a command multiple times, using bash shell?How to keep script from swallowing all of stdin?Pressing Ctrl-C stops bash script, but don't stop PHP script called by the scriptSFTP script gets different destination response when run from different directoriesPHP script error 'exec(): Unable to fork…' on Ubuntu 14.04.3 LTSCreate a screen. Run said screen as a user. Detach












3















I have a few test scripts, each of which runs a test PHP app. Each script runs forever.



So, cat.sh, dog.sh, and foo.sh each run a PHP script. Each shell script runs the PHP app in a loop, so it runs forever, sleeping after each run.



I'm trying to figure out how to run the scripts in parallel at the same time. See the output of the PHP apps in the stdout/term window.



I thought, doing something like the following might work:



foo.sh > &2
dog.sh > &2
cat.sh > &2


In a shell script it would be sufficient, but it's not working.



foo.sh, runs foo.php once, and it runs correctly
dog.sh, runs dog.php in a never ending loop. it runs as expected
cat.sh, runs cat.php in a never ending loop
*** this never runs!!!


It appears that the shell script never gets to run cat.sh. If I run cat.sh by itself in a separate window/term, it runs as expected.



Does anyone have any ideas on how I can get these scripts to run in parallel?










share|improve this question





























    3















    I have a few test scripts, each of which runs a test PHP app. Each script runs forever.



    So, cat.sh, dog.sh, and foo.sh each run a PHP script. Each shell script runs the PHP app in a loop, so it runs forever, sleeping after each run.



    I'm trying to figure out how to run the scripts in parallel at the same time. See the output of the PHP apps in the stdout/term window.



    I thought, doing something like the following might work:



    foo.sh > &2
    dog.sh > &2
    cat.sh > &2


    In a shell script it would be sufficient, but it's not working.



    foo.sh, runs foo.php once, and it runs correctly
    dog.sh, runs dog.php in a never ending loop. it runs as expected
    cat.sh, runs cat.php in a never ending loop
    *** this never runs!!!


    It appears that the shell script never gets to run cat.sh. If I run cat.sh by itself in a separate window/term, it runs as expected.



    Does anyone have any ideas on how I can get these scripts to run in parallel?










    share|improve this question



























      3












      3








      3


      3






      I have a few test scripts, each of which runs a test PHP app. Each script runs forever.



      So, cat.sh, dog.sh, and foo.sh each run a PHP script. Each shell script runs the PHP app in a loop, so it runs forever, sleeping after each run.



      I'm trying to figure out how to run the scripts in parallel at the same time. See the output of the PHP apps in the stdout/term window.



      I thought, doing something like the following might work:



      foo.sh > &2
      dog.sh > &2
      cat.sh > &2


      In a shell script it would be sufficient, but it's not working.



      foo.sh, runs foo.php once, and it runs correctly
      dog.sh, runs dog.php in a never ending loop. it runs as expected
      cat.sh, runs cat.php in a never ending loop
      *** this never runs!!!


      It appears that the shell script never gets to run cat.sh. If I run cat.sh by itself in a separate window/term, it runs as expected.



      Does anyone have any ideas on how I can get these scripts to run in parallel?










      share|improve this question
















      I have a few test scripts, each of which runs a test PHP app. Each script runs forever.



      So, cat.sh, dog.sh, and foo.sh each run a PHP script. Each shell script runs the PHP app in a loop, so it runs forever, sleeping after each run.



      I'm trying to figure out how to run the scripts in parallel at the same time. See the output of the PHP apps in the stdout/term window.



      I thought, doing something like the following might work:



      foo.sh > &2
      dog.sh > &2
      cat.sh > &2


      In a shell script it would be sufficient, but it's not working.



      foo.sh, runs foo.php once, and it runs correctly
      dog.sh, runs dog.php in a never ending loop. it runs as expected
      cat.sh, runs cat.php in a never ending loop
      *** this never runs!!!


      It appears that the shell script never gets to run cat.sh. If I run cat.sh by itself in a separate window/term, it runs as expected.



      Does anyone have any ideas on how I can get these scripts to run in parallel?







      bash shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '12 at 14:04









      Linger

      1863823




      1863823










      asked Mar 27 '12 at 21:08









      tom smithtom smith

      2562510




      2562510






















          5 Answers
          5






          active

          oldest

          votes


















          10














          Your current setup is running your commands sequentially. The first one runs, and completes, allowing the second one to start. The second one never ends, so the third one never gets a chance to start.



          Try this:



          foo.sh >foo.out 2>foo.err &
          dog.sh >dog.out 2>dog.err &
          cat.sh >cat.out 2>cat.err &


          That will tell each command to run in the background, and send their output and error information to files. When a command is run in the background, it detaches from the current session, and allows the next command to run. It's important to put the ampersand (&) as the last character of the line. Otherwise, it might not get recognized correctly.



          Alternately, have each of your shell scripts run the associated .php script you're apparently calling with the same semantics as above. In this scenario, your foo.sh might contain:



          #!/bin/bash
          php foo.php >foo.out 2>foo.err &


          Finally, if you need true parallel operation, check out GNU Parallel.



          Note: If you really want the output going to the screen/terminal, you can leave off the output redirection:



          foo.sh &


          Remember, though, that running multiple commands concurrently can cause confusing output (you may now know what output or errors came from which command), and if you logout, the output has nowhere to go.






          share|improve this answer


























          • Hi Chris. Thanks for the reply. given that my scenario has each shell test running forever, i really don't want to write the stdout/stderr to a file, rather, i would like to see the output/stderr just displyed to the screen... i thought running in the background, meant i didn't have to wait for completion before running the next script.. have i missed something.. || also as to the acceptance rate.. i've tried to delete a bunch of things in the past but never could... in other cases, the answers were totally off base and no help

            – tom smith
            Mar 28 '12 at 0:24











          • Running in the background does mean you don't have to wait for completion before running the next script/command. That's what the & at the end of the command does. If you don't place the ampersand at the end, it doesn't mean the same thing. Redirecting output to a file (especially for background commands) is usually a good thing, unless they send logs somewhere else (like syslog). Otherwise, it's easy to get output mixed up between the multiple commands, since they're running at the same time. I'll update the answer to add a note about output to the terminal.

            – Christopher Cashell
            Mar 28 '12 at 1:49



















          7














          This can be done with parallel command.



          Beautiful examples: parallel



          [me@neo]<bash><~> 19
          05:51 Tue Mar 05 > man -f parallel
          parallel (1) - run programs in parallel


          --



          parallel -j 3 -- "sh foo.sh" "sh dog.sh" "sh cat.sh"


          This will start 3 scripts in parallel, at the same time.



          parallel sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


          This will start by default as many process of the same command, depending of cpu count.



          parallel -j 3 sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


          This will force to execute 3 processes at the same time, even if you have just 2 cpu's.



          Example:



          [me@neo]<bash><~> 31 
          06:09 Tue Mar 05 > parallel -j 10 sh -c "echo Hello World; sleep 3; echo Good morning" -- $(seq 1 10)

          Hello World
          Hello World
          Hello World
          Hello World
          Hello World
          Hello World
          Hello World
          Hello World
          Hello World
          Hello World
          Good morning
          Good morning
          Good morning
          Good morning
          Good morning
          Good morning
          Good morning
          Good morning
          Good morning
          Good morning





          share|improve this answer

































            2














            Chris has the good general idea for most scripts: end each command with & and they'll go into the background. That said, here's my something I keep around for some higher-end tasks. Let me introduce you to my handy toolbox script, parallel-exec.pl



                #!/usr/bin/perl
            use threads;
            use Thread::Queue;

            my @workers;
            my $num_threads = shift;
            my $dbname = shift;
            my $queue = new Thread::Queue;

            for (0..$num_threads-1) {
            $workers[$_] = new threads(&worker);
            print "TEST!n";
            }

            while ($_ = shift @ARGV) {
            $queue->enqueue($_);
            }

            sub worker() {
            while ($file = $queue->dequeue) {
            system ('./parser.pl', $dbname, $file);
            }
            }

            for (0..$num_threads-1) { $queue->enqueue(undef); }
            for (0..$num_threads-1) { $workers[$_]->join; }


            With some light tweaking, you can change the number of threads, provide an array of commands, etc. As this stands right now, it takes the number of threads specified on the command line, a database name, and a list of files to import, then spawns one instance for each file running the specified number at once.






            share|improve this answer



















            • 2





              GNU Parallel provides very similar functionality, but that's a handy little bit of Perl. It never hurts to have multiple options in your toolbox.

              – Christopher Cashell
              Mar 28 '12 at 15:45



















            0














            or..you can do it like this:



            threads=20
            tempfifo=$PMS_HOME/$$.fifo

            trap "exec 1000>&-;exec 1000<&-;exit 0" 2
            mkfifo $tempfifo
            exec 1000<>$tempfifo
            rm -rf $tempfifo

            for ((i=1; i<=$threads; i++))
            do
            echo >&1000
            done

            for ((j=1; j<=1000; j++))
            do
            read -u1000
            {
            echo $j
            echo >&1000
            } &
            done

            wait
            echo "done!!!!!!!!!!"


            every time, it runs 20 threads in parallel.



            hope it help :)






            share|improve this answer
























            • I'd include a #! line at the start of the script. The loop writing the initial lines to the fifo should be done in the background to avoid a potential deadlock (just make it done &).

              – kasperd
              Dec 28 '15 at 9:58



















            0














            Try this :
            ./Cat.sh &
            ./Dog.sh &
            ./foo.sh & they will run in background.



            For more control on the processes embbed them on a python script using multiprocessing module



            import multiprocessing as mp
            import os
            def Run(script_name):
            os.system('./'+script _name+'.sh)
            Pool=[]
            For s in [your script list]:
            p=mp.Process(target =Run ,args=(s,))
            Pool.append(p)
            For p in P:
            p.start()
            Fort p in P:
            p.join()






            share|improve this answer








            New contributor




            Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "2"
              };
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fserverfault.com%2fquestions%2f374131%2fhow-to-run-multiple-shell-scripts-in-parallel%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              10














              Your current setup is running your commands sequentially. The first one runs, and completes, allowing the second one to start. The second one never ends, so the third one never gets a chance to start.



              Try this:



              foo.sh >foo.out 2>foo.err &
              dog.sh >dog.out 2>dog.err &
              cat.sh >cat.out 2>cat.err &


              That will tell each command to run in the background, and send their output and error information to files. When a command is run in the background, it detaches from the current session, and allows the next command to run. It's important to put the ampersand (&) as the last character of the line. Otherwise, it might not get recognized correctly.



              Alternately, have each of your shell scripts run the associated .php script you're apparently calling with the same semantics as above. In this scenario, your foo.sh might contain:



              #!/bin/bash
              php foo.php >foo.out 2>foo.err &


              Finally, if you need true parallel operation, check out GNU Parallel.



              Note: If you really want the output going to the screen/terminal, you can leave off the output redirection:



              foo.sh &


              Remember, though, that running multiple commands concurrently can cause confusing output (you may now know what output or errors came from which command), and if you logout, the output has nowhere to go.






              share|improve this answer


























              • Hi Chris. Thanks for the reply. given that my scenario has each shell test running forever, i really don't want to write the stdout/stderr to a file, rather, i would like to see the output/stderr just displyed to the screen... i thought running in the background, meant i didn't have to wait for completion before running the next script.. have i missed something.. || also as to the acceptance rate.. i've tried to delete a bunch of things in the past but never could... in other cases, the answers were totally off base and no help

                – tom smith
                Mar 28 '12 at 0:24











              • Running in the background does mean you don't have to wait for completion before running the next script/command. That's what the & at the end of the command does. If you don't place the ampersand at the end, it doesn't mean the same thing. Redirecting output to a file (especially for background commands) is usually a good thing, unless they send logs somewhere else (like syslog). Otherwise, it's easy to get output mixed up between the multiple commands, since they're running at the same time. I'll update the answer to add a note about output to the terminal.

                – Christopher Cashell
                Mar 28 '12 at 1:49
















              10














              Your current setup is running your commands sequentially. The first one runs, and completes, allowing the second one to start. The second one never ends, so the third one never gets a chance to start.



              Try this:



              foo.sh >foo.out 2>foo.err &
              dog.sh >dog.out 2>dog.err &
              cat.sh >cat.out 2>cat.err &


              That will tell each command to run in the background, and send their output and error information to files. When a command is run in the background, it detaches from the current session, and allows the next command to run. It's important to put the ampersand (&) as the last character of the line. Otherwise, it might not get recognized correctly.



              Alternately, have each of your shell scripts run the associated .php script you're apparently calling with the same semantics as above. In this scenario, your foo.sh might contain:



              #!/bin/bash
              php foo.php >foo.out 2>foo.err &


              Finally, if you need true parallel operation, check out GNU Parallel.



              Note: If you really want the output going to the screen/terminal, you can leave off the output redirection:



              foo.sh &


              Remember, though, that running multiple commands concurrently can cause confusing output (you may now know what output or errors came from which command), and if you logout, the output has nowhere to go.






              share|improve this answer


























              • Hi Chris. Thanks for the reply. given that my scenario has each shell test running forever, i really don't want to write the stdout/stderr to a file, rather, i would like to see the output/stderr just displyed to the screen... i thought running in the background, meant i didn't have to wait for completion before running the next script.. have i missed something.. || also as to the acceptance rate.. i've tried to delete a bunch of things in the past but never could... in other cases, the answers were totally off base and no help

                – tom smith
                Mar 28 '12 at 0:24











              • Running in the background does mean you don't have to wait for completion before running the next script/command. That's what the & at the end of the command does. If you don't place the ampersand at the end, it doesn't mean the same thing. Redirecting output to a file (especially for background commands) is usually a good thing, unless they send logs somewhere else (like syslog). Otherwise, it's easy to get output mixed up between the multiple commands, since they're running at the same time. I'll update the answer to add a note about output to the terminal.

                – Christopher Cashell
                Mar 28 '12 at 1:49














              10












              10








              10







              Your current setup is running your commands sequentially. The first one runs, and completes, allowing the second one to start. The second one never ends, so the third one never gets a chance to start.



              Try this:



              foo.sh >foo.out 2>foo.err &
              dog.sh >dog.out 2>dog.err &
              cat.sh >cat.out 2>cat.err &


              That will tell each command to run in the background, and send their output and error information to files. When a command is run in the background, it detaches from the current session, and allows the next command to run. It's important to put the ampersand (&) as the last character of the line. Otherwise, it might not get recognized correctly.



              Alternately, have each of your shell scripts run the associated .php script you're apparently calling with the same semantics as above. In this scenario, your foo.sh might contain:



              #!/bin/bash
              php foo.php >foo.out 2>foo.err &


              Finally, if you need true parallel operation, check out GNU Parallel.



              Note: If you really want the output going to the screen/terminal, you can leave off the output redirection:



              foo.sh &


              Remember, though, that running multiple commands concurrently can cause confusing output (you may now know what output or errors came from which command), and if you logout, the output has nowhere to go.






              share|improve this answer















              Your current setup is running your commands sequentially. The first one runs, and completes, allowing the second one to start. The second one never ends, so the third one never gets a chance to start.



              Try this:



              foo.sh >foo.out 2>foo.err &
              dog.sh >dog.out 2>dog.err &
              cat.sh >cat.out 2>cat.err &


              That will tell each command to run in the background, and send their output and error information to files. When a command is run in the background, it detaches from the current session, and allows the next command to run. It's important to put the ampersand (&) as the last character of the line. Otherwise, it might not get recognized correctly.



              Alternately, have each of your shell scripts run the associated .php script you're apparently calling with the same semantics as above. In this scenario, your foo.sh might contain:



              #!/bin/bash
              php foo.php >foo.out 2>foo.err &


              Finally, if you need true parallel operation, check out GNU Parallel.



              Note: If you really want the output going to the screen/terminal, you can leave off the output redirection:



              foo.sh &


              Remember, though, that running multiple commands concurrently can cause confusing output (you may now know what output or errors came from which command), and if you logout, the output has nowhere to go.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 28 '12 at 1:57

























              answered Mar 27 '12 at 23:37









              Christopher CashellChristopher Cashell

              8,14212742




              8,14212742













              • Hi Chris. Thanks for the reply. given that my scenario has each shell test running forever, i really don't want to write the stdout/stderr to a file, rather, i would like to see the output/stderr just displyed to the screen... i thought running in the background, meant i didn't have to wait for completion before running the next script.. have i missed something.. || also as to the acceptance rate.. i've tried to delete a bunch of things in the past but never could... in other cases, the answers were totally off base and no help

                – tom smith
                Mar 28 '12 at 0:24











              • Running in the background does mean you don't have to wait for completion before running the next script/command. That's what the & at the end of the command does. If you don't place the ampersand at the end, it doesn't mean the same thing. Redirecting output to a file (especially for background commands) is usually a good thing, unless they send logs somewhere else (like syslog). Otherwise, it's easy to get output mixed up between the multiple commands, since they're running at the same time. I'll update the answer to add a note about output to the terminal.

                – Christopher Cashell
                Mar 28 '12 at 1:49



















              • Hi Chris. Thanks for the reply. given that my scenario has each shell test running forever, i really don't want to write the stdout/stderr to a file, rather, i would like to see the output/stderr just displyed to the screen... i thought running in the background, meant i didn't have to wait for completion before running the next script.. have i missed something.. || also as to the acceptance rate.. i've tried to delete a bunch of things in the past but never could... in other cases, the answers were totally off base and no help

                – tom smith
                Mar 28 '12 at 0:24











              • Running in the background does mean you don't have to wait for completion before running the next script/command. That's what the & at the end of the command does. If you don't place the ampersand at the end, it doesn't mean the same thing. Redirecting output to a file (especially for background commands) is usually a good thing, unless they send logs somewhere else (like syslog). Otherwise, it's easy to get output mixed up between the multiple commands, since they're running at the same time. I'll update the answer to add a note about output to the terminal.

                – Christopher Cashell
                Mar 28 '12 at 1:49

















              Hi Chris. Thanks for the reply. given that my scenario has each shell test running forever, i really don't want to write the stdout/stderr to a file, rather, i would like to see the output/stderr just displyed to the screen... i thought running in the background, meant i didn't have to wait for completion before running the next script.. have i missed something.. || also as to the acceptance rate.. i've tried to delete a bunch of things in the past but never could... in other cases, the answers were totally off base and no help

              – tom smith
              Mar 28 '12 at 0:24





              Hi Chris. Thanks for the reply. given that my scenario has each shell test running forever, i really don't want to write the stdout/stderr to a file, rather, i would like to see the output/stderr just displyed to the screen... i thought running in the background, meant i didn't have to wait for completion before running the next script.. have i missed something.. || also as to the acceptance rate.. i've tried to delete a bunch of things in the past but never could... in other cases, the answers were totally off base and no help

              – tom smith
              Mar 28 '12 at 0:24













              Running in the background does mean you don't have to wait for completion before running the next script/command. That's what the & at the end of the command does. If you don't place the ampersand at the end, it doesn't mean the same thing. Redirecting output to a file (especially for background commands) is usually a good thing, unless they send logs somewhere else (like syslog). Otherwise, it's easy to get output mixed up between the multiple commands, since they're running at the same time. I'll update the answer to add a note about output to the terminal.

              – Christopher Cashell
              Mar 28 '12 at 1:49





              Running in the background does mean you don't have to wait for completion before running the next script/command. That's what the & at the end of the command does. If you don't place the ampersand at the end, it doesn't mean the same thing. Redirecting output to a file (especially for background commands) is usually a good thing, unless they send logs somewhere else (like syslog). Otherwise, it's easy to get output mixed up between the multiple commands, since they're running at the same time. I'll update the answer to add a note about output to the terminal.

              – Christopher Cashell
              Mar 28 '12 at 1:49













              7














              This can be done with parallel command.



              Beautiful examples: parallel



              [me@neo]<bash><~> 19
              05:51 Tue Mar 05 > man -f parallel
              parallel (1) - run programs in parallel


              --



              parallel -j 3 -- "sh foo.sh" "sh dog.sh" "sh cat.sh"


              This will start 3 scripts in parallel, at the same time.



              parallel sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


              This will start by default as many process of the same command, depending of cpu count.



              parallel -j 3 sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


              This will force to execute 3 processes at the same time, even if you have just 2 cpu's.



              Example:



              [me@neo]<bash><~> 31 
              06:09 Tue Mar 05 > parallel -j 10 sh -c "echo Hello World; sleep 3; echo Good morning" -- $(seq 1 10)

              Hello World
              Hello World
              Hello World
              Hello World
              Hello World
              Hello World
              Hello World
              Hello World
              Hello World
              Hello World
              Good morning
              Good morning
              Good morning
              Good morning
              Good morning
              Good morning
              Good morning
              Good morning
              Good morning
              Good morning





              share|improve this answer






























                7














                This can be done with parallel command.



                Beautiful examples: parallel



                [me@neo]<bash><~> 19
                05:51 Tue Mar 05 > man -f parallel
                parallel (1) - run programs in parallel


                --



                parallel -j 3 -- "sh foo.sh" "sh dog.sh" "sh cat.sh"


                This will start 3 scripts in parallel, at the same time.



                parallel sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


                This will start by default as many process of the same command, depending of cpu count.



                parallel -j 3 sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


                This will force to execute 3 processes at the same time, even if you have just 2 cpu's.



                Example:



                [me@neo]<bash><~> 31 
                06:09 Tue Mar 05 > parallel -j 10 sh -c "echo Hello World; sleep 3; echo Good morning" -- $(seq 1 10)

                Hello World
                Hello World
                Hello World
                Hello World
                Hello World
                Hello World
                Hello World
                Hello World
                Hello World
                Hello World
                Good morning
                Good morning
                Good morning
                Good morning
                Good morning
                Good morning
                Good morning
                Good morning
                Good morning
                Good morning





                share|improve this answer




























                  7












                  7








                  7







                  This can be done with parallel command.



                  Beautiful examples: parallel



                  [me@neo]<bash><~> 19
                  05:51 Tue Mar 05 > man -f parallel
                  parallel (1) - run programs in parallel


                  --



                  parallel -j 3 -- "sh foo.sh" "sh dog.sh" "sh cat.sh"


                  This will start 3 scripts in parallel, at the same time.



                  parallel sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


                  This will start by default as many process of the same command, depending of cpu count.



                  parallel -j 3 sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


                  This will force to execute 3 processes at the same time, even if you have just 2 cpu's.



                  Example:



                  [me@neo]<bash><~> 31 
                  06:09 Tue Mar 05 > parallel -j 10 sh -c "echo Hello World; sleep 3; echo Good morning" -- $(seq 1 10)

                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning





                  share|improve this answer















                  This can be done with parallel command.



                  Beautiful examples: parallel



                  [me@neo]<bash><~> 19
                  05:51 Tue Mar 05 > man -f parallel
                  parallel (1) - run programs in parallel


                  --



                  parallel -j 3 -- "sh foo.sh" "sh dog.sh" "sh cat.sh"


                  This will start 3 scripts in parallel, at the same time.



                  parallel sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


                  This will start by default as many process of the same command, depending of cpu count.



                  parallel -j 3 sh -c "echo hi; sleep 2; echo bye" -- 1 2 3


                  This will force to execute 3 processes at the same time, even if you have just 2 cpu's.



                  Example:



                  [me@neo]<bash><~> 31 
                  06:09 Tue Mar 05 > parallel -j 10 sh -c "echo Hello World; sleep 3; echo Good morning" -- $(seq 1 10)

                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Hello World
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning
                  Good morning






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 5 '13 at 4:15

























                  answered Mar 5 '13 at 3:59







                  user133922






























                      2














                      Chris has the good general idea for most scripts: end each command with & and they'll go into the background. That said, here's my something I keep around for some higher-end tasks. Let me introduce you to my handy toolbox script, parallel-exec.pl



                          #!/usr/bin/perl
                      use threads;
                      use Thread::Queue;

                      my @workers;
                      my $num_threads = shift;
                      my $dbname = shift;
                      my $queue = new Thread::Queue;

                      for (0..$num_threads-1) {
                      $workers[$_] = new threads(&worker);
                      print "TEST!n";
                      }

                      while ($_ = shift @ARGV) {
                      $queue->enqueue($_);
                      }

                      sub worker() {
                      while ($file = $queue->dequeue) {
                      system ('./parser.pl', $dbname, $file);
                      }
                      }

                      for (0..$num_threads-1) { $queue->enqueue(undef); }
                      for (0..$num_threads-1) { $workers[$_]->join; }


                      With some light tweaking, you can change the number of threads, provide an array of commands, etc. As this stands right now, it takes the number of threads specified on the command line, a database name, and a list of files to import, then spawns one instance for each file running the specified number at once.






                      share|improve this answer



















                      • 2





                        GNU Parallel provides very similar functionality, but that's a handy little bit of Perl. It never hurts to have multiple options in your toolbox.

                        – Christopher Cashell
                        Mar 28 '12 at 15:45
















                      2














                      Chris has the good general idea for most scripts: end each command with & and they'll go into the background. That said, here's my something I keep around for some higher-end tasks. Let me introduce you to my handy toolbox script, parallel-exec.pl



                          #!/usr/bin/perl
                      use threads;
                      use Thread::Queue;

                      my @workers;
                      my $num_threads = shift;
                      my $dbname = shift;
                      my $queue = new Thread::Queue;

                      for (0..$num_threads-1) {
                      $workers[$_] = new threads(&worker);
                      print "TEST!n";
                      }

                      while ($_ = shift @ARGV) {
                      $queue->enqueue($_);
                      }

                      sub worker() {
                      while ($file = $queue->dequeue) {
                      system ('./parser.pl', $dbname, $file);
                      }
                      }

                      for (0..$num_threads-1) { $queue->enqueue(undef); }
                      for (0..$num_threads-1) { $workers[$_]->join; }


                      With some light tweaking, you can change the number of threads, provide an array of commands, etc. As this stands right now, it takes the number of threads specified on the command line, a database name, and a list of files to import, then spawns one instance for each file running the specified number at once.






                      share|improve this answer



















                      • 2





                        GNU Parallel provides very similar functionality, but that's a handy little bit of Perl. It never hurts to have multiple options in your toolbox.

                        – Christopher Cashell
                        Mar 28 '12 at 15:45














                      2












                      2








                      2







                      Chris has the good general idea for most scripts: end each command with & and they'll go into the background. That said, here's my something I keep around for some higher-end tasks. Let me introduce you to my handy toolbox script, parallel-exec.pl



                          #!/usr/bin/perl
                      use threads;
                      use Thread::Queue;

                      my @workers;
                      my $num_threads = shift;
                      my $dbname = shift;
                      my $queue = new Thread::Queue;

                      for (0..$num_threads-1) {
                      $workers[$_] = new threads(&worker);
                      print "TEST!n";
                      }

                      while ($_ = shift @ARGV) {
                      $queue->enqueue($_);
                      }

                      sub worker() {
                      while ($file = $queue->dequeue) {
                      system ('./parser.pl', $dbname, $file);
                      }
                      }

                      for (0..$num_threads-1) { $queue->enqueue(undef); }
                      for (0..$num_threads-1) { $workers[$_]->join; }


                      With some light tweaking, you can change the number of threads, provide an array of commands, etc. As this stands right now, it takes the number of threads specified on the command line, a database name, and a list of files to import, then spawns one instance for each file running the specified number at once.






                      share|improve this answer













                      Chris has the good general idea for most scripts: end each command with & and they'll go into the background. That said, here's my something I keep around for some higher-end tasks. Let me introduce you to my handy toolbox script, parallel-exec.pl



                          #!/usr/bin/perl
                      use threads;
                      use Thread::Queue;

                      my @workers;
                      my $num_threads = shift;
                      my $dbname = shift;
                      my $queue = new Thread::Queue;

                      for (0..$num_threads-1) {
                      $workers[$_] = new threads(&worker);
                      print "TEST!n";
                      }

                      while ($_ = shift @ARGV) {
                      $queue->enqueue($_);
                      }

                      sub worker() {
                      while ($file = $queue->dequeue) {
                      system ('./parser.pl', $dbname, $file);
                      }
                      }

                      for (0..$num_threads-1) { $queue->enqueue(undef); }
                      for (0..$num_threads-1) { $workers[$_]->join; }


                      With some light tweaking, you can change the number of threads, provide an array of commands, etc. As this stands right now, it takes the number of threads specified on the command line, a database name, and a list of files to import, then spawns one instance for each file running the specified number at once.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 28 '12 at 7:26









                      Jeff FerlandJeff Ferland

                      18.4k14978




                      18.4k14978








                      • 2





                        GNU Parallel provides very similar functionality, but that's a handy little bit of Perl. It never hurts to have multiple options in your toolbox.

                        – Christopher Cashell
                        Mar 28 '12 at 15:45














                      • 2





                        GNU Parallel provides very similar functionality, but that's a handy little bit of Perl. It never hurts to have multiple options in your toolbox.

                        – Christopher Cashell
                        Mar 28 '12 at 15:45








                      2




                      2





                      GNU Parallel provides very similar functionality, but that's a handy little bit of Perl. It never hurts to have multiple options in your toolbox.

                      – Christopher Cashell
                      Mar 28 '12 at 15:45





                      GNU Parallel provides very similar functionality, but that's a handy little bit of Perl. It never hurts to have multiple options in your toolbox.

                      – Christopher Cashell
                      Mar 28 '12 at 15:45











                      0














                      or..you can do it like this:



                      threads=20
                      tempfifo=$PMS_HOME/$$.fifo

                      trap "exec 1000>&-;exec 1000<&-;exit 0" 2
                      mkfifo $tempfifo
                      exec 1000<>$tempfifo
                      rm -rf $tempfifo

                      for ((i=1; i<=$threads; i++))
                      do
                      echo >&1000
                      done

                      for ((j=1; j<=1000; j++))
                      do
                      read -u1000
                      {
                      echo $j
                      echo >&1000
                      } &
                      done

                      wait
                      echo "done!!!!!!!!!!"


                      every time, it runs 20 threads in parallel.



                      hope it help :)






                      share|improve this answer
























                      • I'd include a #! line at the start of the script. The loop writing the initial lines to the fifo should be done in the background to avoid a potential deadlock (just make it done &).

                        – kasperd
                        Dec 28 '15 at 9:58
















                      0














                      or..you can do it like this:



                      threads=20
                      tempfifo=$PMS_HOME/$$.fifo

                      trap "exec 1000>&-;exec 1000<&-;exit 0" 2
                      mkfifo $tempfifo
                      exec 1000<>$tempfifo
                      rm -rf $tempfifo

                      for ((i=1; i<=$threads; i++))
                      do
                      echo >&1000
                      done

                      for ((j=1; j<=1000; j++))
                      do
                      read -u1000
                      {
                      echo $j
                      echo >&1000
                      } &
                      done

                      wait
                      echo "done!!!!!!!!!!"


                      every time, it runs 20 threads in parallel.



                      hope it help :)






                      share|improve this answer
























                      • I'd include a #! line at the start of the script. The loop writing the initial lines to the fifo should be done in the background to avoid a potential deadlock (just make it done &).

                        – kasperd
                        Dec 28 '15 at 9:58














                      0












                      0








                      0







                      or..you can do it like this:



                      threads=20
                      tempfifo=$PMS_HOME/$$.fifo

                      trap "exec 1000>&-;exec 1000<&-;exit 0" 2
                      mkfifo $tempfifo
                      exec 1000<>$tempfifo
                      rm -rf $tempfifo

                      for ((i=1; i<=$threads; i++))
                      do
                      echo >&1000
                      done

                      for ((j=1; j<=1000; j++))
                      do
                      read -u1000
                      {
                      echo $j
                      echo >&1000
                      } &
                      done

                      wait
                      echo "done!!!!!!!!!!"


                      every time, it runs 20 threads in parallel.



                      hope it help :)






                      share|improve this answer













                      or..you can do it like this:



                      threads=20
                      tempfifo=$PMS_HOME/$$.fifo

                      trap "exec 1000>&-;exec 1000<&-;exit 0" 2
                      mkfifo $tempfifo
                      exec 1000<>$tempfifo
                      rm -rf $tempfifo

                      for ((i=1; i<=$threads; i++))
                      do
                      echo >&1000
                      done

                      for ((j=1; j<=1000; j++))
                      do
                      read -u1000
                      {
                      echo $j
                      echo >&1000
                      } &
                      done

                      wait
                      echo "done!!!!!!!!!!"


                      every time, it runs 20 threads in parallel.



                      hope it help :)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 28 '15 at 9:16









                      ouyangyeweiouyangyewei

                      1




                      1













                      • I'd include a #! line at the start of the script. The loop writing the initial lines to the fifo should be done in the background to avoid a potential deadlock (just make it done &).

                        – kasperd
                        Dec 28 '15 at 9:58



















                      • I'd include a #! line at the start of the script. The loop writing the initial lines to the fifo should be done in the background to avoid a potential deadlock (just make it done &).

                        – kasperd
                        Dec 28 '15 at 9:58

















                      I'd include a #! line at the start of the script. The loop writing the initial lines to the fifo should be done in the background to avoid a potential deadlock (just make it done &).

                      – kasperd
                      Dec 28 '15 at 9:58





                      I'd include a #! line at the start of the script. The loop writing the initial lines to the fifo should be done in the background to avoid a potential deadlock (just make it done &).

                      – kasperd
                      Dec 28 '15 at 9:58











                      0














                      Try this :
                      ./Cat.sh &
                      ./Dog.sh &
                      ./foo.sh & they will run in background.



                      For more control on the processes embbed them on a python script using multiprocessing module



                      import multiprocessing as mp
                      import os
                      def Run(script_name):
                      os.system('./'+script _name+'.sh)
                      Pool=[]
                      For s in [your script list]:
                      p=mp.Process(target =Run ,args=(s,))
                      Pool.append(p)
                      For p in P:
                      p.start()
                      Fort p in P:
                      p.join()






                      share|improve this answer








                      New contributor




                      Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

























                        0














                        Try this :
                        ./Cat.sh &
                        ./Dog.sh &
                        ./foo.sh & they will run in background.



                        For more control on the processes embbed them on a python script using multiprocessing module



                        import multiprocessing as mp
                        import os
                        def Run(script_name):
                        os.system('./'+script _name+'.sh)
                        Pool=[]
                        For s in [your script list]:
                        p=mp.Process(target =Run ,args=(s,))
                        Pool.append(p)
                        For p in P:
                        p.start()
                        Fort p in P:
                        p.join()






                        share|improve this answer








                        New contributor




                        Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.























                          0












                          0








                          0







                          Try this :
                          ./Cat.sh &
                          ./Dog.sh &
                          ./foo.sh & they will run in background.



                          For more control on the processes embbed them on a python script using multiprocessing module



                          import multiprocessing as mp
                          import os
                          def Run(script_name):
                          os.system('./'+script _name+'.sh)
                          Pool=[]
                          For s in [your script list]:
                          p=mp.Process(target =Run ,args=(s,))
                          Pool.append(p)
                          For p in P:
                          p.start()
                          Fort p in P:
                          p.join()






                          share|improve this answer








                          New contributor




                          Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.










                          Try this :
                          ./Cat.sh &
                          ./Dog.sh &
                          ./foo.sh & they will run in background.



                          For more control on the processes embbed them on a python script using multiprocessing module



                          import multiprocessing as mp
                          import os
                          def Run(script_name):
                          os.system('./'+script _name+'.sh)
                          Pool=[]
                          For s in [your script list]:
                          p=mp.Process(target =Run ,args=(s,))
                          Pool.append(p)
                          For p in P:
                          p.start()
                          Fort p in P:
                          p.join()







                          share|improve this answer








                          New contributor




                          Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 10 mins ago









                          EddieEddie

                          1




                          1




                          New contributor




                          Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Eddie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Server Fault!


                              • 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%2fserverfault.com%2fquestions%2f374131%2fhow-to-run-multiple-shell-scripts-in-parallel%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

                              117736 Шеррод Примітки | Див. також | Посилання | Навігаційне...

                              As a Security Precaution, the user account has been locked The Next CEO of Stack OverflowMS...

                              Маріан Котлеба Зміст Життєпис | Політичні погляди |...