How to make any program work with the tee command?

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











up vote
1
down vote

favorite












I wrote a program (in Ruby) and it worked well. It prints out a few lines of text and then pause for a minute and then repeats the above.



It works well (in Mac OS X's terminal), but I noted that if I wanted to keep a record of the output, by using



ruby myscript.rb | tee record.txt


then the output won't be shown on the screen, until the output reaches a certain amount, perhaps a few kb. However, this could mean 5 or 10 minutes with nothing on the screen (and nothing in the file either if I press CTRL-C).



I could modify the program to flush the output, but I thought an app should be agnostic as to what is being used with the output to STDOUT. So can tee be made to work well (perhaps with an option), or any other command could be used, or maybe it is an option of Bash or the Terminal app? Is there a way?







share|improve this question


























    up vote
    1
    down vote

    favorite












    I wrote a program (in Ruby) and it worked well. It prints out a few lines of text and then pause for a minute and then repeats the above.



    It works well (in Mac OS X's terminal), but I noted that if I wanted to keep a record of the output, by using



    ruby myscript.rb | tee record.txt


    then the output won't be shown on the screen, until the output reaches a certain amount, perhaps a few kb. However, this could mean 5 or 10 minutes with nothing on the screen (and nothing in the file either if I press CTRL-C).



    I could modify the program to flush the output, but I thought an app should be agnostic as to what is being used with the output to STDOUT. So can tee be made to work well (perhaps with an option), or any other command could be used, or maybe it is an option of Bash or the Terminal app? Is there a way?







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I wrote a program (in Ruby) and it worked well. It prints out a few lines of text and then pause for a minute and then repeats the above.



      It works well (in Mac OS X's terminal), but I noted that if I wanted to keep a record of the output, by using



      ruby myscript.rb | tee record.txt


      then the output won't be shown on the screen, until the output reaches a certain amount, perhaps a few kb. However, this could mean 5 or 10 minutes with nothing on the screen (and nothing in the file either if I press CTRL-C).



      I could modify the program to flush the output, but I thought an app should be agnostic as to what is being used with the output to STDOUT. So can tee be made to work well (perhaps with an option), or any other command could be used, or maybe it is an option of Bash or the Terminal app? Is there a way?







      share|improve this question














      I wrote a program (in Ruby) and it worked well. It prints out a few lines of text and then pause for a minute and then repeats the above.



      It works well (in Mac OS X's terminal), but I noted that if I wanted to keep a record of the output, by using



      ruby myscript.rb | tee record.txt


      then the output won't be shown on the screen, until the output reaches a certain amount, perhaps a few kb. However, this could mean 5 or 10 minutes with nothing on the screen (and nothing in the file either if I press CTRL-C).



      I could modify the program to flush the output, but I thought an app should be agnostic as to what is being used with the output to STDOUT. So can tee be made to work well (perhaps with an option), or any other command could be used, or maybe it is an option of Bash or the Terminal app? Is there a way?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '17 at 23:21

























      asked Nov 28 '17 at 10:24









      太極者無極而生

      79741124




      79741124




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          Please look at the answer https://stackoverflow.com/a/11337109/5656721 as it's a common issue. You could use stdbuff or unbuffer for your ruby program as well.






          share|improve this answer



























            up vote
            1
            down vote














            I thought an app should be agnostic as to what is being used with the output to STDOUT.




            You thought wrongly. The runtime libraries of several programming languages, including the C and C++ languages, all share the semantics of changing the buffering according to whether they detect that the stream is attached to a terminal device.



            This is a defined part of the language in many cases. The C language standard says, for example:


            As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.


            The usual choice if a stream is not attached to a terminal device is to select unit buffering, line buffering, or full buffering. Unit buffering and line buffering are usual for standard error; full buffering for standard output. But this does vary by language.



            So to get the output to be not fully buffered when the output stream is a pipe in such lanugages you have to



            • write your program to itself make standard output line buffered, smart buffered, or unbuffered;

            • use things that hook your particular programming language's runtime library, such as the stdbuf command if your programming language is C or uses (the I/O streams part of) the C runtime library as its base (which some programming languages do not); or

            • use tools such as ptybandage that make the program think that its standard output is a terminal, whilst taking that output and sending it to the write end of the pipe.

            Further reading



            • https://unix.stackexchange.com/a/249801/5132





            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%2f407455%2fhow-to-make-any-program-work-with-the-tee-command%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              Please look at the answer https://stackoverflow.com/a/11337109/5656721 as it's a common issue. You could use stdbuff or unbuffer for your ruby program as well.






              share|improve this answer
























                up vote
                2
                down vote













                Please look at the answer https://stackoverflow.com/a/11337109/5656721 as it's a common issue. You could use stdbuff or unbuffer for your ruby program as well.






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Please look at the answer https://stackoverflow.com/a/11337109/5656721 as it's a common issue. You could use stdbuff or unbuffer for your ruby program as well.






                  share|improve this answer












                  Please look at the answer https://stackoverflow.com/a/11337109/5656721 as it's a common issue. You could use stdbuff or unbuffer for your ruby program as well.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '17 at 10:36









                  tifssoft

                  5015




                  5015






















                      up vote
                      1
                      down vote














                      I thought an app should be agnostic as to what is being used with the output to STDOUT.




                      You thought wrongly. The runtime libraries of several programming languages, including the C and C++ languages, all share the semantics of changing the buffering according to whether they detect that the stream is attached to a terminal device.



                      This is a defined part of the language in many cases. The C language standard says, for example:


                      As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.


                      The usual choice if a stream is not attached to a terminal device is to select unit buffering, line buffering, or full buffering. Unit buffering and line buffering are usual for standard error; full buffering for standard output. But this does vary by language.



                      So to get the output to be not fully buffered when the output stream is a pipe in such lanugages you have to



                      • write your program to itself make standard output line buffered, smart buffered, or unbuffered;

                      • use things that hook your particular programming language's runtime library, such as the stdbuf command if your programming language is C or uses (the I/O streams part of) the C runtime library as its base (which some programming languages do not); or

                      • use tools such as ptybandage that make the program think that its standard output is a terminal, whilst taking that output and sending it to the write end of the pipe.

                      Further reading



                      • https://unix.stackexchange.com/a/249801/5132





                      share|improve this answer
























                        up vote
                        1
                        down vote














                        I thought an app should be agnostic as to what is being used with the output to STDOUT.




                        You thought wrongly. The runtime libraries of several programming languages, including the C and C++ languages, all share the semantics of changing the buffering according to whether they detect that the stream is attached to a terminal device.



                        This is a defined part of the language in many cases. The C language standard says, for example:


                        As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.


                        The usual choice if a stream is not attached to a terminal device is to select unit buffering, line buffering, or full buffering. Unit buffering and line buffering are usual for standard error; full buffering for standard output. But this does vary by language.



                        So to get the output to be not fully buffered when the output stream is a pipe in such lanugages you have to



                        • write your program to itself make standard output line buffered, smart buffered, or unbuffered;

                        • use things that hook your particular programming language's runtime library, such as the stdbuf command if your programming language is C or uses (the I/O streams part of) the C runtime library as its base (which some programming languages do not); or

                        • use tools such as ptybandage that make the program think that its standard output is a terminal, whilst taking that output and sending it to the write end of the pipe.

                        Further reading



                        • https://unix.stackexchange.com/a/249801/5132





                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote










                          I thought an app should be agnostic as to what is being used with the output to STDOUT.




                          You thought wrongly. The runtime libraries of several programming languages, including the C and C++ languages, all share the semantics of changing the buffering according to whether they detect that the stream is attached to a terminal device.



                          This is a defined part of the language in many cases. The C language standard says, for example:


                          As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.


                          The usual choice if a stream is not attached to a terminal device is to select unit buffering, line buffering, or full buffering. Unit buffering and line buffering are usual for standard error; full buffering for standard output. But this does vary by language.



                          So to get the output to be not fully buffered when the output stream is a pipe in such lanugages you have to



                          • write your program to itself make standard output line buffered, smart buffered, or unbuffered;

                          • use things that hook your particular programming language's runtime library, such as the stdbuf command if your programming language is C or uses (the I/O streams part of) the C runtime library as its base (which some programming languages do not); or

                          • use tools such as ptybandage that make the program think that its standard output is a terminal, whilst taking that output and sending it to the write end of the pipe.

                          Further reading



                          • https://unix.stackexchange.com/a/249801/5132





                          share|improve this answer













                          I thought an app should be agnostic as to what is being used with the output to STDOUT.




                          You thought wrongly. The runtime libraries of several programming languages, including the C and C++ languages, all share the semantics of changing the buffering according to whether they detect that the stream is attached to a terminal device.



                          This is a defined part of the language in many cases. The C language standard says, for example:


                          As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.


                          The usual choice if a stream is not attached to a terminal device is to select unit buffering, line buffering, or full buffering. Unit buffering and line buffering are usual for standard error; full buffering for standard output. But this does vary by language.



                          So to get the output to be not fully buffered when the output stream is a pipe in such lanugages you have to



                          • write your program to itself make standard output line buffered, smart buffered, or unbuffered;

                          • use things that hook your particular programming language's runtime library, such as the stdbuf command if your programming language is C or uses (the I/O streams part of) the C runtime library as its base (which some programming languages do not); or

                          • use tools such as ptybandage that make the program think that its standard output is a terminal, whilst taking that output and sending it to the write end of the pipe.

                          Further reading



                          • https://unix.stackexchange.com/a/249801/5132






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 28 '17 at 11:18









                          JdeBP

                          28.8k459135




                          28.8k459135



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407455%2fhow-to-make-any-program-work-with-the-tee-command%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