Send copy of a script's output to a file

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











up vote
9
down vote

favorite
2












Say I have a Zsh script and that I would like to let it print output to STDOUT, but also copy (dump) its output to a file in disk.



Moreover, the script starts with the following option



set -o xtrace


which forces it to be verbose and print what commands it runs. I would like to capture this output as well in a file in disk.



My understanding is that if I do



./my_script.sh > log.txt


it will just send STDOUT to log.txt, but what if I want to also be able to see the output in the terminal?



I have read about tee and the MULTIOS option in Zsh, but am not sure how to use them.



When I do:



./my_script | tee log.txt


I can see the output on the terminal, but the file log.txt doesn'tseem to be capturing everything (in fact it captures barely anything).







share|improve this question






















  • ./my_script.sh > log.txt 2>&1
    – mikeserv
    Jun 5 '14 at 22:59










  • Looks like you're looking for the script command. Or maybe myscript >&1 > log.txt 2>&1
    – Stéphane Chazelas
    Jun 7 '14 at 20:20















up vote
9
down vote

favorite
2












Say I have a Zsh script and that I would like to let it print output to STDOUT, but also copy (dump) its output to a file in disk.



Moreover, the script starts with the following option



set -o xtrace


which forces it to be verbose and print what commands it runs. I would like to capture this output as well in a file in disk.



My understanding is that if I do



./my_script.sh > log.txt


it will just send STDOUT to log.txt, but what if I want to also be able to see the output in the terminal?



I have read about tee and the MULTIOS option in Zsh, but am not sure how to use them.



When I do:



./my_script | tee log.txt


I can see the output on the terminal, but the file log.txt doesn'tseem to be capturing everything (in fact it captures barely anything).







share|improve this question






















  • ./my_script.sh > log.txt 2>&1
    – mikeserv
    Jun 5 '14 at 22:59










  • Looks like you're looking for the script command. Or maybe myscript >&1 > log.txt 2>&1
    – Stéphane Chazelas
    Jun 7 '14 at 20:20













up vote
9
down vote

favorite
2









up vote
9
down vote

favorite
2






2





Say I have a Zsh script and that I would like to let it print output to STDOUT, but also copy (dump) its output to a file in disk.



Moreover, the script starts with the following option



set -o xtrace


which forces it to be verbose and print what commands it runs. I would like to capture this output as well in a file in disk.



My understanding is that if I do



./my_script.sh > log.txt


it will just send STDOUT to log.txt, but what if I want to also be able to see the output in the terminal?



I have read about tee and the MULTIOS option in Zsh, but am not sure how to use them.



When I do:



./my_script | tee log.txt


I can see the output on the terminal, but the file log.txt doesn'tseem to be capturing everything (in fact it captures barely anything).







share|improve this question














Say I have a Zsh script and that I would like to let it print output to STDOUT, but also copy (dump) its output to a file in disk.



Moreover, the script starts with the following option



set -o xtrace


which forces it to be verbose and print what commands it runs. I would like to capture this output as well in a file in disk.



My understanding is that if I do



./my_script.sh > log.txt


it will just send STDOUT to log.txt, but what if I want to also be able to see the output in the terminal?



I have read about tee and the MULTIOS option in Zsh, but am not sure how to use them.



When I do:



./my_script | tee log.txt


I can see the output on the terminal, but the file log.txt doesn'tseem to be capturing everything (in fact it captures barely anything).









share|improve this question













share|improve this question




share|improve this question








edited Jun 5 '14 at 23:42









Gilles

504k1199971523




504k1199971523










asked Jun 5 '14 at 19:37









Amelio Vazquez-Reina

11.7k48124225




11.7k48124225











  • ./my_script.sh > log.txt 2>&1
    – mikeserv
    Jun 5 '14 at 22:59










  • Looks like you're looking for the script command. Or maybe myscript >&1 > log.txt 2>&1
    – Stéphane Chazelas
    Jun 7 '14 at 20:20

















  • ./my_script.sh > log.txt 2>&1
    – mikeserv
    Jun 5 '14 at 22:59










  • Looks like you're looking for the script command. Or maybe myscript >&1 > log.txt 2>&1
    – Stéphane Chazelas
    Jun 7 '14 at 20:20
















./my_script.sh > log.txt 2>&1
– mikeserv
Jun 5 '14 at 22:59




./my_script.sh > log.txt 2>&1
– mikeserv
Jun 5 '14 at 22:59












Looks like you're looking for the script command. Or maybe myscript >&1 > log.txt 2>&1
– Stéphane Chazelas
Jun 7 '14 at 20:20





Looks like you're looking for the script command. Or maybe myscript >&1 > log.txt 2>&1
– Stéphane Chazelas
Jun 7 '14 at 20:20











2 Answers
2






active

oldest

votes

















up vote
10
down vote



accepted










It could be that your script is producing output to stdout and stderr, and you are only getting one of those streams output to your log file.



./my_script.sh | tee log.txt will indeed output everything to the terminal, but will only dump stdout to the logfile.



./my_script.sh > log.txt 2>&1 will do the opposite, dumping everything to the log file, but displaying nothing on screen.



The trick is to combine the two with tee:



./myscript.sh 2>&1 | tee log.txt


This redirects stderr (2) into stdout (1), then pipes stdout into tee, which copies it to the terminal and to the log file.



The zsh multios equivalent would be:



./myscript.sh >&1 > log.txt 2>&1


That is, redirect stdout both to the original stdout and log.txt (internally via a pipe to something that works like tee), and then redirect stderr to that as well (to the pipe to the internal tee-like process).






share|improve this answer






















  • Thanks -- Regarding your last line, why not ./myscript.sh >&1 2>&1 > log.txt ? (i.e. switching the order of the last two redirections). Would there be any difference between them?
    – Amelio Vazquez-Reina
    Jun 9 '14 at 12:45











  • Your variant does not output onto stdout, just to log.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
    – savanto
    Jun 9 '14 at 16:28

















up vote
0
down vote













nohup allows a job to carry on even if the console dies or is closed, useful for lengthy backups etc, but here we are using its automatic logging.



nohup myscript.sh & ; tail -f nohup.out





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%2f134734%2fsend-copy-of-a-scripts-output-to-a-file%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
    10
    down vote



    accepted










    It could be that your script is producing output to stdout and stderr, and you are only getting one of those streams output to your log file.



    ./my_script.sh | tee log.txt will indeed output everything to the terminal, but will only dump stdout to the logfile.



    ./my_script.sh > log.txt 2>&1 will do the opposite, dumping everything to the log file, but displaying nothing on screen.



    The trick is to combine the two with tee:



    ./myscript.sh 2>&1 | tee log.txt


    This redirects stderr (2) into stdout (1), then pipes stdout into tee, which copies it to the terminal and to the log file.



    The zsh multios equivalent would be:



    ./myscript.sh >&1 > log.txt 2>&1


    That is, redirect stdout both to the original stdout and log.txt (internally via a pipe to something that works like tee), and then redirect stderr to that as well (to the pipe to the internal tee-like process).






    share|improve this answer






















    • Thanks -- Regarding your last line, why not ./myscript.sh >&1 2>&1 > log.txt ? (i.e. switching the order of the last two redirections). Would there be any difference between them?
      – Amelio Vazquez-Reina
      Jun 9 '14 at 12:45











    • Your variant does not output onto stdout, just to log.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
      – savanto
      Jun 9 '14 at 16:28














    up vote
    10
    down vote



    accepted










    It could be that your script is producing output to stdout and stderr, and you are only getting one of those streams output to your log file.



    ./my_script.sh | tee log.txt will indeed output everything to the terminal, but will only dump stdout to the logfile.



    ./my_script.sh > log.txt 2>&1 will do the opposite, dumping everything to the log file, but displaying nothing on screen.



    The trick is to combine the two with tee:



    ./myscript.sh 2>&1 | tee log.txt


    This redirects stderr (2) into stdout (1), then pipes stdout into tee, which copies it to the terminal and to the log file.



    The zsh multios equivalent would be:



    ./myscript.sh >&1 > log.txt 2>&1


    That is, redirect stdout both to the original stdout and log.txt (internally via a pipe to something that works like tee), and then redirect stderr to that as well (to the pipe to the internal tee-like process).






    share|improve this answer






















    • Thanks -- Regarding your last line, why not ./myscript.sh >&1 2>&1 > log.txt ? (i.e. switching the order of the last two redirections). Would there be any difference between them?
      – Amelio Vazquez-Reina
      Jun 9 '14 at 12:45











    • Your variant does not output onto stdout, just to log.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
      – savanto
      Jun 9 '14 at 16:28












    up vote
    10
    down vote



    accepted







    up vote
    10
    down vote



    accepted






    It could be that your script is producing output to stdout and stderr, and you are only getting one of those streams output to your log file.



    ./my_script.sh | tee log.txt will indeed output everything to the terminal, but will only dump stdout to the logfile.



    ./my_script.sh > log.txt 2>&1 will do the opposite, dumping everything to the log file, but displaying nothing on screen.



    The trick is to combine the two with tee:



    ./myscript.sh 2>&1 | tee log.txt


    This redirects stderr (2) into stdout (1), then pipes stdout into tee, which copies it to the terminal and to the log file.



    The zsh multios equivalent would be:



    ./myscript.sh >&1 > log.txt 2>&1


    That is, redirect stdout both to the original stdout and log.txt (internally via a pipe to something that works like tee), and then redirect stderr to that as well (to the pipe to the internal tee-like process).






    share|improve this answer














    It could be that your script is producing output to stdout and stderr, and you are only getting one of those streams output to your log file.



    ./my_script.sh | tee log.txt will indeed output everything to the terminal, but will only dump stdout to the logfile.



    ./my_script.sh > log.txt 2>&1 will do the opposite, dumping everything to the log file, but displaying nothing on screen.



    The trick is to combine the two with tee:



    ./myscript.sh 2>&1 | tee log.txt


    This redirects stderr (2) into stdout (1), then pipes stdout into tee, which copies it to the terminal and to the log file.



    The zsh multios equivalent would be:



    ./myscript.sh >&1 > log.txt 2>&1


    That is, redirect stdout both to the original stdout and log.txt (internally via a pipe to something that works like tee), and then redirect stderr to that as well (to the pipe to the internal tee-like process).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 7 '14 at 20:26









    Stéphane Chazelas

    280k53514846




    280k53514846










    answered Jun 5 '14 at 23:36









    savanto

    40336




    40336











    • Thanks -- Regarding your last line, why not ./myscript.sh >&1 2>&1 > log.txt ? (i.e. switching the order of the last two redirections). Would there be any difference between them?
      – Amelio Vazquez-Reina
      Jun 9 '14 at 12:45











    • Your variant does not output onto stdout, just to log.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
      – savanto
      Jun 9 '14 at 16:28
















    • Thanks -- Regarding your last line, why not ./myscript.sh >&1 2>&1 > log.txt ? (i.e. switching the order of the last two redirections). Would there be any difference between them?
      – Amelio Vazquez-Reina
      Jun 9 '14 at 12:45











    • Your variant does not output onto stdout, just to log.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
      – savanto
      Jun 9 '14 at 16:28















    Thanks -- Regarding your last line, why not ./myscript.sh >&1 2>&1 > log.txt ? (i.e. switching the order of the last two redirections). Would there be any difference between them?
    – Amelio Vazquez-Reina
    Jun 9 '14 at 12:45





    Thanks -- Regarding your last line, why not ./myscript.sh >&1 2>&1 > log.txt ? (i.e. switching the order of the last two redirections). Would there be any difference between them?
    – Amelio Vazquez-Reina
    Jun 9 '14 at 12:45













    Your variant does not output onto stdout, just to log.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
    – savanto
    Jun 9 '14 at 16:28




    Your variant does not output onto stdout, just to log.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
    – savanto
    Jun 9 '14 at 16:28












    up vote
    0
    down vote













    nohup allows a job to carry on even if the console dies or is closed, useful for lengthy backups etc, but here we are using its automatic logging.



    nohup myscript.sh & ; tail -f nohup.out





    share|improve this answer


























      up vote
      0
      down vote













      nohup allows a job to carry on even if the console dies or is closed, useful for lengthy backups etc, but here we are using its automatic logging.



      nohup myscript.sh & ; tail -f nohup.out





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        nohup allows a job to carry on even if the console dies or is closed, useful for lengthy backups etc, but here we are using its automatic logging.



        nohup myscript.sh & ; tail -f nohup.out





        share|improve this answer














        nohup allows a job to carry on even if the console dies or is closed, useful for lengthy backups etc, but here we are using its automatic logging.



        nohup myscript.sh & ; tail -f nohup.out






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 6 '16 at 10:44

























        answered Dec 6 '16 at 10:27









        zzapper

        689510




        689510






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f134734%2fsend-copy-of-a-scripts-output-to-a-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?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?