Send copy of a script's output to a file

Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
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).
shell-script zsh io-redirection pipe output
add a comment |Â
up vote
9
down vote
favorite
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).
shell-script zsh io-redirection pipe output
./my_script.sh > log.txt 2>&1
â mikeserv
Jun 5 '14 at 22:59
Looks like you're looking for thescriptcommand. Or maybemyscript >&1 > log.txt 2>&1
â Stéphane Chazelas
Jun 7 '14 at 20:20
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
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).
shell-script zsh io-redirection pipe output
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).
shell-script zsh io-redirection pipe output
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 thescriptcommand. Or maybemyscript >&1 > log.txt 2>&1
â Stéphane Chazelas
Jun 7 '14 at 20:20
add a comment |Â
./my_script.sh > log.txt 2>&1
â mikeserv
Jun 5 '14 at 22:59
Looks like you're looking for thescriptcommand. Or maybemyscript >&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
add a comment |Â
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).
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 ontostdout, just tolog.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
â savanto
Jun 9 '14 at 16:28
add a comment |Â
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
add a comment |Â
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).
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 ontostdout, just tolog.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
â savanto
Jun 9 '14 at 16:28
add a comment |Â
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).
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 ontostdout, just tolog.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
â savanto
Jun 9 '14 at 16:28
add a comment |Â
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).
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).
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 ontostdout, just tolog.txt. The last line in the answer (added by @StéphaneChazelas and not myself) outputs to both.
â savanto
Jun 9 '14 at 16:28
add a comment |Â
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 ontostdout, just tolog.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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Dec 6 '16 at 10:44
answered Dec 6 '16 at 10:27
zzapper
689510
689510
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
./my_script.sh > log.txt 2>&1â mikeserv
Jun 5 '14 at 22:59
Looks like you're looking for the
scriptcommand. Or maybemyscript >&1 > log.txt 2>&1â Stéphane Chazelas
Jun 7 '14 at 20:20