Log commands execution time

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











up vote
6
down vote

favorite
1












I'd like to log execution time of my commands. Something like this:



#!/usr/bin/env bash
echo "$@" >> /tmp/times
exec 3>&2
(/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times


The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?







share|improve this question


























    up vote
    6
    down vote

    favorite
    1












    I'd like to log execution time of my commands. Something like this:



    #!/usr/bin/env bash
    echo "$@" >> /tmp/times
    exec 3>&2
    (/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times


    The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?







    share|improve this question
























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      I'd like to log execution time of my commands. Something like this:



      #!/usr/bin/env bash
      echo "$@" >> /tmp/times
      exec 3>&2
      (/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times


      The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?







      share|improve this question














      I'd like to log execution time of my commands. Something like this:



      #!/usr/bin/env bash
      echo "$@" >> /tmp/times
      exec 3>&2
      (/usr/bin/time -f "%e" "$@" 2>&3) 2>>/tmp/times


      The problem is that time spawns child process that polluted standard error. Can this be done without this side effect?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 22 '17 at 21:09









      Gilles

      508k12010031533




      508k12010031533










      asked Oct 22 '17 at 20:29









      sevo

      557313




      557313




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.



          The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.



          #!/bin/sh
          echo "$@" >>/tmp/times
          exec time -f %e -a -o /tmp/times -- "$@"


          Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.



          #!/bin/bash
          echo "$@" >>/tmp/times
          TIMEFORMAT='%R'
          time "$@" 2>&3; 3>&2 2>>/tmp/times





          share|improve this answer




















          • After a quick test, only the secon works for me. GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.
            – Nikos Alexandris
            Nov 3 '17 at 8:37










          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%2f399779%2flog-commands-execution-time%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          8
          down vote



          accepted










          If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.



          The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.



          #!/bin/sh
          echo "$@" >>/tmp/times
          exec time -f %e -a -o /tmp/times -- "$@"


          Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.



          #!/bin/bash
          echo "$@" >>/tmp/times
          TIMEFORMAT='%R'
          time "$@" 2>&3; 3>&2 2>>/tmp/times





          share|improve this answer




















          • After a quick test, only the secon works for me. GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.
            – Nikos Alexandris
            Nov 3 '17 at 8:37














          up vote
          8
          down vote



          accepted










          If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.



          The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.



          #!/bin/sh
          echo "$@" >>/tmp/times
          exec time -f %e -a -o /tmp/times -- "$@"


          Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.



          #!/bin/bash
          echo "$@" >>/tmp/times
          TIMEFORMAT='%R'
          time "$@" 2>&3; 3>&2 2>>/tmp/times





          share|improve this answer




















          • After a quick test, only the secon works for me. GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.
            – Nikos Alexandris
            Nov 3 '17 at 8:37












          up vote
          8
          down vote



          accepted







          up vote
          8
          down vote



          accepted






          If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.



          The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.



          #!/bin/sh
          echo "$@" >>/tmp/times
          exec time -f %e -a -o /tmp/times -- "$@"


          Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.



          #!/bin/bash
          echo "$@" >>/tmp/times
          TIMEFORMAT='%R'
          time "$@" 2>&3; 3>&2 2>>/tmp/times





          share|improve this answer












          If you use /usr/bin/time, then the only way to send the timed process's stderr and the time information to different channels is to tell the time utility to do it. You can't do it with redirections in the shell that calls time because the timed process's stderr and the output from time itself are sent to the same file descriptor.



          The time utility that you're using (based on your use of -f, it's GNU coreutils) has an option -o to tell it to write to a different file, and -a to append to this file. This option also exists on FreeBSD and macOS.



          #!/bin/sh
          echo "$@" >>/tmp/times
          exec time -f %e -a -o /tmp/times -- "$@"


          Alternatively, you can use the shell keyword (in shells that have it, which includes bash, ksh and zsh). With the keyword, you can control where the time information goes through redirection, because you can specify a redirection as part of the timed process.



          #!/bin/bash
          echo "$@" >>/tmp/times
          TIMEFORMAT='%R'
          time "$@" 2>&3; 3>&2 2>>/tmp/times






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 22 '17 at 21:09









          Gilles

          508k12010031533




          508k12010031533











          • After a quick test, only the secon works for me. GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.
            – Nikos Alexandris
            Nov 3 '17 at 8:37
















          • After a quick test, only the secon works for me. GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.
            – Nikos Alexandris
            Nov 3 '17 at 8:37















          After a quick test, only the secon works for me. GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.
          – Nikos Alexandris
          Nov 3 '17 at 8:37




          After a quick test, only the secon works for me. GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu) under Funtoo-Linux.
          – Nikos Alexandris
          Nov 3 '17 at 8:37

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399779%2flog-commands-execution-time%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)