1

This command does not store the output of my script in the log file:

. myscript.sh | tee -a log_file.log

However this next command does store the output in the log file.

. myscript.sh 2>&1 | tee -a dash_log.log

Why does the second one work and not the first? AFAIK the only difference is that I have told bash to redirect stderr to stdout, and it should not make any difference to what happens with stdout. Yet that first command would not even store the 'error-less' version of stdout in the log file.

And in case it matters: I'm on Ubuntu 12.04 hosted on AWS

EDIT: This was caused by erroneous assumptions of how wget outputs messages. See https://stackoverflow.com/questions/13066518/why-does-wget-output-to-stderr-rather-than-stdout and http://www.gnu.org/software/wget/manual/html_node/Logging-and-Input-File-Options.html

6
  • can i see the script ? Sep 9, 2013 at 2:53
  • I'd rather not include the script actually. The script starts with source ~/.bashrc, then loops through some urls calling wget "theurl.com" --output-document="local/dir/theurl.com" for each one. All the script output comes from wget.
    – Trindaz
    Sep 9, 2013 at 3:45
  • and there are no errors from wget, with each entry in the log being of the form something liketimestamp - url; transfer messages; "Saving to: ..."; 0K .......... 31.9M=0s
    – Trindaz
    Sep 9, 2013 at 3:47
  • do you have anywhere this: 1>&2? Either in the script itself or in .bashrc which you are sourcing. Sep 9, 2013 at 4:12
  • @DanilaLadner no that doesn't appear in the script
    – Trindaz
    Sep 9, 2013 at 6:27

2 Answers 2

4

It seems likely that your script is writing most or all of it's output to stderr. This is easy to test

myscript.sh > std.out
myscript.sh 2> err.out

then look at the contents of each file and be educated.


I doubt it - the only output from the script comes from calls to wget

Instead of doubting, again this is easy to test

$ wget http://serverfault.com >std.out
--2013-09-09 07:06:25--  http://serverfault.com/
Resolving serverfault.com... 198.252.206.16
Connecting to serverfault.com|198.252.206.16|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 51867 (51K) [text/html]
Saving to: “index.html.6”

100%[=================================================>] 51,867   231K/s  in 0.2s

2013-09-09 07:06:26 (231 KB/s) - “index.html.6” saved [51867/51867]

See, output still gets written to the terminal then try

 $ wget http://serverfault.com 2>err.out
 $

Q.E.D.

The wget command writes it's output to stderr not stdout.

3
  • I stand corrected. While it's not clear at all to me why wget would publish "standard" output messages that are not about errors to stderr, that is indeed what it is doing. Not a safe assumption to make at all.
    – Trindaz
    Sep 9, 2013 at 6:32
  • I've always figured it writes to STDERR so that if you wget -o - the messages from wget wont get mixed with the payload.
    – Petter H
    Sep 9, 2013 at 6:51
  • @PetterH good point - I did discover that on reading the man page, but it still doesn't feel right. I've continued discussion at ubuntuforums.org/showthread.php?t=2173318
    – Trindaz
    Sep 9, 2013 at 6:58
1

Would probably need to see the code but it is probably due to your code writing out to stderr.

1
  • I doubt it - the only output from the script comes from calls to wget, which completes normally multiple times during the script.
    – Trindaz
    Sep 9, 2013 at 3:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .