Question: I have a build bash script running under Hudson build system, which writes its own log file. However, Hudson captures all the stdout and stderr of the build script it executes and displays it as the "console output" of the build. Furthermore, this output is saved in the build history.

How can I access this "console output" from within the script itself? I'd like to 1) save it as log together with the artifacts; 2) attach it to the notification email. Thanks

Answer: 
1. It is saved along with the artifacts (the `log` file in top-level of build's directory, i.e. `jobs/`*jobname*`/builds/`*buildid*`/log`).
2. It is automatically added to the email hudson sends, though truncated from begining.

If you need to get it anywhere else, there are two options:

1. You can have to wrap the script in a block and pipe it's output through tee. So you'd convert:

```
#!/bin/sh
make this
make that

```

to:

```
#!/bin/bash
{
    make this
    make that
} 2>&1 | tee output
# Now the output is in file 'output' while Hudson did see it.

```

Unfortunately I am not sure how to force line-buffering in `tee` so the real-time log printing works in Hudson (at least my cygwin version does not mention `-u` option).
2. You could use the [Groovy plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin) and/or [Groovy Postbuild plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin) to access the internal API. The "system" groovy script in build step and the post-build groovy script both have access to the build object (though in slightly different way) of type [hudson.model.AbstractBuild](http://javadoc.jenkins-ci.org/hudson/model/AbstractBuild.html) and from that you can get the content of the console using the [hudson.model.Run#getLog(int)](http://javadoc.jenkins-ci.org/hudson/model/Run.html#getLog(int)) method.
To force line-buffering in tee, just start the program with `unbuffer {command}` which is part of the `expect` package. Another alternative to `unbuffer` is to use `stdbuf -eL -oL {command}`