How to run time on multiple commands AND write the time output to file?

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











up vote
59
down vote

favorite
9












I want to run time command to measure time of several commands.



What I want to do is:



  • Measure the time of running of all of them added together

  • Write the time output to a file

  • Write the STDERR from the command I am measuring to STDERR

What I do NOT want to do is



  • Write the several commands into a separate script (why? because all of this is already a script that I am generating programatically, and creating ANOTHER temporary script would be more mess than I want)

What I tried so far:



/usr/bin/time --output=outtime -p echo "a"; echo "b";



Doesn't work, time is run only on the first one.



/usr/bin/time --output=outtime -p ( echo "a"; echo "b"; )



Doesn't work, ( is unexpected token.



/usr/bin/time --output=outtime -p echo "a"; echo "b";



Doesn't work, "no such file or directory".



/usr/bin/time --output=outtime -p ' echo "a"; echo "b";'



Doesn't work, "no such file or directory".



time ( echo "a"; echo "b"; ) 2>outtime



Doesn't work, since it redirects all STDERR into outtime; I want only the time output there.



And of course,



time --output=outime echo "a";



Doesn't work, since --output=outime: command not found.



How can I do it?










share|improve this question



























    up vote
    59
    down vote

    favorite
    9












    I want to run time command to measure time of several commands.



    What I want to do is:



    • Measure the time of running of all of them added together

    • Write the time output to a file

    • Write the STDERR from the command I am measuring to STDERR

    What I do NOT want to do is



    • Write the several commands into a separate script (why? because all of this is already a script that I am generating programatically, and creating ANOTHER temporary script would be more mess than I want)

    What I tried so far:



    /usr/bin/time --output=outtime -p echo "a"; echo "b";



    Doesn't work, time is run only on the first one.



    /usr/bin/time --output=outtime -p ( echo "a"; echo "b"; )



    Doesn't work, ( is unexpected token.



    /usr/bin/time --output=outtime -p echo "a"; echo "b";



    Doesn't work, "no such file or directory".



    /usr/bin/time --output=outtime -p ' echo "a"; echo "b";'



    Doesn't work, "no such file or directory".



    time ( echo "a"; echo "b"; ) 2>outtime



    Doesn't work, since it redirects all STDERR into outtime; I want only the time output there.



    And of course,



    time --output=outime echo "a";



    Doesn't work, since --output=outime: command not found.



    How can I do it?










    share|improve this question

























      up vote
      59
      down vote

      favorite
      9









      up vote
      59
      down vote

      favorite
      9






      9





      I want to run time command to measure time of several commands.



      What I want to do is:



      • Measure the time of running of all of them added together

      • Write the time output to a file

      • Write the STDERR from the command I am measuring to STDERR

      What I do NOT want to do is



      • Write the several commands into a separate script (why? because all of this is already a script that I am generating programatically, and creating ANOTHER temporary script would be more mess than I want)

      What I tried so far:



      /usr/bin/time --output=outtime -p echo "a"; echo "b";



      Doesn't work, time is run only on the first one.



      /usr/bin/time --output=outtime -p ( echo "a"; echo "b"; )



      Doesn't work, ( is unexpected token.



      /usr/bin/time --output=outtime -p echo "a"; echo "b";



      Doesn't work, "no such file or directory".



      /usr/bin/time --output=outtime -p ' echo "a"; echo "b";'



      Doesn't work, "no such file or directory".



      time ( echo "a"; echo "b"; ) 2>outtime



      Doesn't work, since it redirects all STDERR into outtime; I want only the time output there.



      And of course,



      time --output=outime echo "a";



      Doesn't work, since --output=outime: command not found.



      How can I do it?










      share|improve this question















      I want to run time command to measure time of several commands.



      What I want to do is:



      • Measure the time of running of all of them added together

      • Write the time output to a file

      • Write the STDERR from the command I am measuring to STDERR

      What I do NOT want to do is



      • Write the several commands into a separate script (why? because all of this is already a script that I am generating programatically, and creating ANOTHER temporary script would be more mess than I want)

      What I tried so far:



      /usr/bin/time --output=outtime -p echo "a"; echo "b";



      Doesn't work, time is run only on the first one.



      /usr/bin/time --output=outtime -p ( echo "a"; echo "b"; )



      Doesn't work, ( is unexpected token.



      /usr/bin/time --output=outtime -p echo "a"; echo "b";



      Doesn't work, "no such file or directory".



      /usr/bin/time --output=outtime -p ' echo "a"; echo "b";'



      Doesn't work, "no such file or directory".



      time ( echo "a"; echo "b"; ) 2>outtime



      Doesn't work, since it redirects all STDERR into outtime; I want only the time output there.



      And of course,



      time --output=outime echo "a";



      Doesn't work, since --output=outime: command not found.



      How can I do it?







      shell command time-utility






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 8 '16 at 23:45









      Gilles

      511k12010091540




      511k12010091540










      asked Aug 22 '12 at 4:14









      Karel Bílek

      83921224




      83921224




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          75
          down vote



          accepted










          Use sh -c 'commands' as the command, e.g.:



          /usr/bin/time --output=outtime -p sh -c 'echo "a"; echo "b"'





          share|improve this answer



























            up vote
            6
            down vote













            Try this:



            % (time ( echas z; echo 2 2>&3 ) ) 3>&2 2>timeoutput
            zsh: command not found: echas
            2
            % cat timeoutput
            ( echas z; echo 2; 2>&3; ) 0.00s user 0.00s system 0% cpu 0.004 total


            Explanation:



            First, we have to find a way to redirect the output of time. Since time is a shell builtin, it takes the full command line as the command to be measured, including redirections. Thus,



            % time whatever 2>timeoutput
            whatever 2> timeoutput 0.00s user 0.00s system 0% cpu 0.018 total
            % cat timeoutput
            zsh: command not found: whatever


            [Note: janos's comment implies this is not the case for bash.] We can achieve the redirection of time's output by running time in a subshell and then redirecting the output of that subshell.



            % (time whatever) 2> timeoutput
            % cat timeoutput
            zsh: command not found: whatever
            whatever 0.00s user 0.00s system 0% cpu 0.018 total


            Now we have successfully redirected the output of time, but its output is mixed with the error output of the command we are measuring. To separate the two, we use an additional file descriptor.



            On the "outside" we have



            % (time ... ) 3>&2 2>timeout


            This means: whatever is written to file descriptor 3, will be output to the same place file descriptor 2 (standard error) is outputting now (the terminal). And then we redirect standard error to the file timeout.



            So now we have: everything written to stdout and fd 3 will go to the terminal, and everything written to stderr will go to the file. What's left is to redirect the measured command's stderr to fd 3.



            % (time whatever 2>&3) 3>&2 2>timeout


            Now, to make time measure more than one command, we need to run them in an(other!) subshell (inside parentheses). And to redirect the error output of all of them to fd 3, we need to group them inside curly brackets.



            So, finally, we arrive at:



            % (time ( whatever; ls 2>&3 ) ) 3>&2 2>timeoutput


            That's it.






            share|improve this answer






















            • This is a syntax error in a POSIX shell. Probably a bashism?
              – josch
              Aug 1 '17 at 6:00










            • @josch the shell used here is zsh.
              – angus
              Aug 28 '17 at 13:22

















            up vote
            1
            down vote













            Not the correct answer but very related to the question.

            Get timing statistics for multiple programs combined parentheses are required. Separate commands with semicolons.



            time ( command1 ; command2 )





            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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f46051%2fhow-to-run-time-on-multiple-commands-and-write-the-time-output-to-file%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              75
              down vote



              accepted










              Use sh -c 'commands' as the command, e.g.:



              /usr/bin/time --output=outtime -p sh -c 'echo "a"; echo "b"'





              share|improve this answer
























                up vote
                75
                down vote



                accepted










                Use sh -c 'commands' as the command, e.g.:



                /usr/bin/time --output=outtime -p sh -c 'echo "a"; echo "b"'





                share|improve this answer






















                  up vote
                  75
                  down vote



                  accepted







                  up vote
                  75
                  down vote



                  accepted






                  Use sh -c 'commands' as the command, e.g.:



                  /usr/bin/time --output=outtime -p sh -c 'echo "a"; echo "b"'





                  share|improve this answer












                  Use sh -c 'commands' as the command, e.g.:



                  /usr/bin/time --output=outtime -p sh -c 'echo "a"; echo "b"'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 22 '12 at 19:36









                  Jim Paris

                  11.1k42330




                  11.1k42330






















                      up vote
                      6
                      down vote













                      Try this:



                      % (time ( echas z; echo 2 2>&3 ) ) 3>&2 2>timeoutput
                      zsh: command not found: echas
                      2
                      % cat timeoutput
                      ( echas z; echo 2; 2>&3; ) 0.00s user 0.00s system 0% cpu 0.004 total


                      Explanation:



                      First, we have to find a way to redirect the output of time. Since time is a shell builtin, it takes the full command line as the command to be measured, including redirections. Thus,



                      % time whatever 2>timeoutput
                      whatever 2> timeoutput 0.00s user 0.00s system 0% cpu 0.018 total
                      % cat timeoutput
                      zsh: command not found: whatever


                      [Note: janos's comment implies this is not the case for bash.] We can achieve the redirection of time's output by running time in a subshell and then redirecting the output of that subshell.



                      % (time whatever) 2> timeoutput
                      % cat timeoutput
                      zsh: command not found: whatever
                      whatever 0.00s user 0.00s system 0% cpu 0.018 total


                      Now we have successfully redirected the output of time, but its output is mixed with the error output of the command we are measuring. To separate the two, we use an additional file descriptor.



                      On the "outside" we have



                      % (time ... ) 3>&2 2>timeout


                      This means: whatever is written to file descriptor 3, will be output to the same place file descriptor 2 (standard error) is outputting now (the terminal). And then we redirect standard error to the file timeout.



                      So now we have: everything written to stdout and fd 3 will go to the terminal, and everything written to stderr will go to the file. What's left is to redirect the measured command's stderr to fd 3.



                      % (time whatever 2>&3) 3>&2 2>timeout


                      Now, to make time measure more than one command, we need to run them in an(other!) subshell (inside parentheses). And to redirect the error output of all of them to fd 3, we need to group them inside curly brackets.



                      So, finally, we arrive at:



                      % (time ( whatever; ls 2>&3 ) ) 3>&2 2>timeoutput


                      That's it.






                      share|improve this answer






















                      • This is a syntax error in a POSIX shell. Probably a bashism?
                        – josch
                        Aug 1 '17 at 6:00










                      • @josch the shell used here is zsh.
                        – angus
                        Aug 28 '17 at 13:22














                      up vote
                      6
                      down vote













                      Try this:



                      % (time ( echas z; echo 2 2>&3 ) ) 3>&2 2>timeoutput
                      zsh: command not found: echas
                      2
                      % cat timeoutput
                      ( echas z; echo 2; 2>&3; ) 0.00s user 0.00s system 0% cpu 0.004 total


                      Explanation:



                      First, we have to find a way to redirect the output of time. Since time is a shell builtin, it takes the full command line as the command to be measured, including redirections. Thus,



                      % time whatever 2>timeoutput
                      whatever 2> timeoutput 0.00s user 0.00s system 0% cpu 0.018 total
                      % cat timeoutput
                      zsh: command not found: whatever


                      [Note: janos's comment implies this is not the case for bash.] We can achieve the redirection of time's output by running time in a subshell and then redirecting the output of that subshell.



                      % (time whatever) 2> timeoutput
                      % cat timeoutput
                      zsh: command not found: whatever
                      whatever 0.00s user 0.00s system 0% cpu 0.018 total


                      Now we have successfully redirected the output of time, but its output is mixed with the error output of the command we are measuring. To separate the two, we use an additional file descriptor.



                      On the "outside" we have



                      % (time ... ) 3>&2 2>timeout


                      This means: whatever is written to file descriptor 3, will be output to the same place file descriptor 2 (standard error) is outputting now (the terminal). And then we redirect standard error to the file timeout.



                      So now we have: everything written to stdout and fd 3 will go to the terminal, and everything written to stderr will go to the file. What's left is to redirect the measured command's stderr to fd 3.



                      % (time whatever 2>&3) 3>&2 2>timeout


                      Now, to make time measure more than one command, we need to run them in an(other!) subshell (inside parentheses). And to redirect the error output of all of them to fd 3, we need to group them inside curly brackets.



                      So, finally, we arrive at:



                      % (time ( whatever; ls 2>&3 ) ) 3>&2 2>timeoutput


                      That's it.






                      share|improve this answer






















                      • This is a syntax error in a POSIX shell. Probably a bashism?
                        – josch
                        Aug 1 '17 at 6:00










                      • @josch the shell used here is zsh.
                        – angus
                        Aug 28 '17 at 13:22












                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      Try this:



                      % (time ( echas z; echo 2 2>&3 ) ) 3>&2 2>timeoutput
                      zsh: command not found: echas
                      2
                      % cat timeoutput
                      ( echas z; echo 2; 2>&3; ) 0.00s user 0.00s system 0% cpu 0.004 total


                      Explanation:



                      First, we have to find a way to redirect the output of time. Since time is a shell builtin, it takes the full command line as the command to be measured, including redirections. Thus,



                      % time whatever 2>timeoutput
                      whatever 2> timeoutput 0.00s user 0.00s system 0% cpu 0.018 total
                      % cat timeoutput
                      zsh: command not found: whatever


                      [Note: janos's comment implies this is not the case for bash.] We can achieve the redirection of time's output by running time in a subshell and then redirecting the output of that subshell.



                      % (time whatever) 2> timeoutput
                      % cat timeoutput
                      zsh: command not found: whatever
                      whatever 0.00s user 0.00s system 0% cpu 0.018 total


                      Now we have successfully redirected the output of time, but its output is mixed with the error output of the command we are measuring. To separate the two, we use an additional file descriptor.



                      On the "outside" we have



                      % (time ... ) 3>&2 2>timeout


                      This means: whatever is written to file descriptor 3, will be output to the same place file descriptor 2 (standard error) is outputting now (the terminal). And then we redirect standard error to the file timeout.



                      So now we have: everything written to stdout and fd 3 will go to the terminal, and everything written to stderr will go to the file. What's left is to redirect the measured command's stderr to fd 3.



                      % (time whatever 2>&3) 3>&2 2>timeout


                      Now, to make time measure more than one command, we need to run them in an(other!) subshell (inside parentheses). And to redirect the error output of all of them to fd 3, we need to group them inside curly brackets.



                      So, finally, we arrive at:



                      % (time ( whatever; ls 2>&3 ) ) 3>&2 2>timeoutput


                      That's it.






                      share|improve this answer














                      Try this:



                      % (time ( echas z; echo 2 2>&3 ) ) 3>&2 2>timeoutput
                      zsh: command not found: echas
                      2
                      % cat timeoutput
                      ( echas z; echo 2; 2>&3; ) 0.00s user 0.00s system 0% cpu 0.004 total


                      Explanation:



                      First, we have to find a way to redirect the output of time. Since time is a shell builtin, it takes the full command line as the command to be measured, including redirections. Thus,



                      % time whatever 2>timeoutput
                      whatever 2> timeoutput 0.00s user 0.00s system 0% cpu 0.018 total
                      % cat timeoutput
                      zsh: command not found: whatever


                      [Note: janos's comment implies this is not the case for bash.] We can achieve the redirection of time's output by running time in a subshell and then redirecting the output of that subshell.



                      % (time whatever) 2> timeoutput
                      % cat timeoutput
                      zsh: command not found: whatever
                      whatever 0.00s user 0.00s system 0% cpu 0.018 total


                      Now we have successfully redirected the output of time, but its output is mixed with the error output of the command we are measuring. To separate the two, we use an additional file descriptor.



                      On the "outside" we have



                      % (time ... ) 3>&2 2>timeout


                      This means: whatever is written to file descriptor 3, will be output to the same place file descriptor 2 (standard error) is outputting now (the terminal). And then we redirect standard error to the file timeout.



                      So now we have: everything written to stdout and fd 3 will go to the terminal, and everything written to stderr will go to the file. What's left is to redirect the measured command's stderr to fd 3.



                      % (time whatever 2>&3) 3>&2 2>timeout


                      Now, to make time measure more than one command, we need to run them in an(other!) subshell (inside parentheses). And to redirect the error output of all of them to fd 3, we need to group them inside curly brackets.



                      So, finally, we arrive at:



                      % (time ( whatever; ls 2>&3 ) ) 3>&2 2>timeoutput


                      That's it.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 23 '12 at 18:00

























                      answered Aug 22 '12 at 6:26









                      angus

                      8,89113332




                      8,89113332











                      • This is a syntax error in a POSIX shell. Probably a bashism?
                        – josch
                        Aug 1 '17 at 6:00










                      • @josch the shell used here is zsh.
                        – angus
                        Aug 28 '17 at 13:22
















                      • This is a syntax error in a POSIX shell. Probably a bashism?
                        – josch
                        Aug 1 '17 at 6:00










                      • @josch the shell used here is zsh.
                        – angus
                        Aug 28 '17 at 13:22















                      This is a syntax error in a POSIX shell. Probably a bashism?
                      – josch
                      Aug 1 '17 at 6:00




                      This is a syntax error in a POSIX shell. Probably a bashism?
                      – josch
                      Aug 1 '17 at 6:00












                      @josch the shell used here is zsh.
                      – angus
                      Aug 28 '17 at 13:22




                      @josch the shell used here is zsh.
                      – angus
                      Aug 28 '17 at 13:22










                      up vote
                      1
                      down vote













                      Not the correct answer but very related to the question.

                      Get timing statistics for multiple programs combined parentheses are required. Separate commands with semicolons.



                      time ( command1 ; command2 )





                      share|improve this answer


























                        up vote
                        1
                        down vote













                        Not the correct answer but very related to the question.

                        Get timing statistics for multiple programs combined parentheses are required. Separate commands with semicolons.



                        time ( command1 ; command2 )





                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Not the correct answer but very related to the question.

                          Get timing statistics for multiple programs combined parentheses are required. Separate commands with semicolons.



                          time ( command1 ; command2 )





                          share|improve this answer














                          Not the correct answer but very related to the question.

                          Get timing statistics for multiple programs combined parentheses are required. Separate commands with semicolons.



                          time ( command1 ; command2 )






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 31 at 10:44









                          ploth

                          848115




                          848115










                          answered Aug 31 at 7:42









                          Martin T.

                          112




                          112



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f46051%2fhow-to-run-time-on-multiple-commands-and-write-the-time-output-to-file%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              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