-> ls -ltr nowhere.txt 2>&1 |tee -a /tmp/error_test.log
nowhere.txt: No such file or directory
-> cat -n /tmp/error_test.log
1 nowhere.txt: No such file or directory
2 nowhere.txt: No such file or directory
If you want to write to two log files, you should combine this with tee:
${PROGRAM} 2>&1 | tee -a "${LOG_FILE_1}" >> ${LOGFILE_2} | log
I just ran this on ksh and bash on AIX, Linux and Solaris, and sh on Solaris, and it works the same on all of them - but only when feeding log through a pipe - not calling it directly. What OS/Shell are you using?
Maybe it would be best if you showed your script - exactly what you are doing - including where you set variables, like LOGFILE, LOG_FILE_1, LOGFILE_2, PROGRAM, etc...
Are you sure following helped you to capture stderr into output.log log file?
It appears you are directing stdout to output.log in a non-appending mode.
The closest solution I have tryed is the following:
log() {
while read LINE; do
echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
done
}
${command} >> "${LOGFILE_1}" 2>>"${LOGFILE_2} | log
I obtain the stdout inside $LOGFILE_1 and the stderr inside $LOGFILE_2 BUT without the date before it, as I wanted to.
Simply the log function is not called at all.
Variables $LOGFILE_1 and $LOGFILE_2 are something like
$command is an executable written in C a bit hard to explain what it does...but it prints strings either on stdout and stderr, I think it is enough to describe my problem.
I tryed also with
log() {
while read LINE; do
echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
done
}
${command} 2>>"${LOGFILE_2} | log
but I obtain the stdout (with date) in the log file and
log() {
while read LINE; do
echo "$(date) - ${LINE}" 2>> "${LOGFILE_2}"
done
}
${command} 2>>"${LOGFILE_2} | log
(notice the "2>>" inside the function), but I obtain the stderr inside the log, WITHOUT the date.
I think that the problem can be summarized into this question: how to pass the stderr to a function?
There is no point in directing output of standard error in your log function using 2>> because there never is any.
SomeFunction 2>&1 >> ${LOGFILE_2} | log
Standard error is directed to where standard output is going at this point in time (i.e. the screen, standard output)
Standard output is then sent to LOGFILE_2
Standard error (still pointing to where standard output was pointing before it was changed) (i.e. the screen) is piped into the log function
The log function should write standard output - not standard error - to LOGFILE
log() {
while read LINE; do
echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
done
}
${command} >> "${LOGFILE_1}" 2>>"${LOGFILE_2} | log
All you are doing here is writing standard output to LOGFILE_1 and standard error to LOGFILE_2. There is nothing left for the log function to do - you left nothing to be piped into it.
log() {
while read LINE; do
echo "$(date) - ${LINE}" >> "${LOGFILE_2}"
done
}
${command} 2>>"${LOGFILE_2} | log
Here, you have sent standard error to LOGFILE_2 (you're missing a quote on that line), standard output goes to the log function, which you also write to LOGFILE_2
log() {
while read LINE; do
echo "$(date) - ${LINE}" 2>> "${LOGFILE_2}"
done
}
${command} 2>>"${LOGFILE_2} | log
And here, you have written standard error to LOGFILE_2, standard output goes to the log function, but you do nothing with it, as you're directing standard error to LOGFILE_2 (although there is nothing in standard error to write to the file)
$ cat Test1
LOGFILE_1=L1.txt
LOGFILE_2=L2.txt
LOGFILE=ERR.txt
log() {
while read LINE; do
echo "$(date) - ${LINE}" 2>> "${LOGFILE}"
done
}
SomeFunction() {
echo "This line goes to log file"
echo "This line goes to error file" >&2
}
SomeFunction 2>&1 >> ${LOGFILE_2} | log
echo "Log"
cat $LOGFILE_2
echo
echo
echo Error
cat $LOGFILE
echo
echo
echo
$ ./Test1
Log
This line goes to log file
Error
$ cat Test2
LOGFILE_1=L1.txt
LOGFILE_2=L2.txt
LOGFILE=ERR.txt
log() {
while read LINE; do
echo "$(date) - ${LINE}" >> "${LOGFILE}"
done
}
SomeFunction() {
echo "This line goes to log file"
echo "This line goes to error file" >&2
}
SomeFunction 2>&1 >> ${LOGFILE_2} | log
echo "Log"
cat $LOGFILE_2
echo
echo
echo Error
cat $LOGFILE
echo
echo
echo
$ ./Test2
Log
This line goes to log file
Error
Thu May 20 12:42:20 DFT 2010 - This line goes to error file
Thank you so much scottn!
Now my script works as I wanted to and I have definetely better understood bash redirection.
I've reimplemented your last example in a way more similar to my case: instead of producing strings on stdout and stderr with bash, I produce them with a C program, then executed by a script.
Here's the C prog:
#include <stdio.h>
#include <unistd.h>
int main()
{
int i;
for(i=0; i<3; i++)
{
fprintf(stdout, "This is message n. %d on the standard output\n", i);
fprintf(stderr, "This is message n. %d on the standard error\n", i);
sleep(1);
}
return 0;
}
After the compilation, I have executed it with the following script:
$ ./stdoutAndstderr.sh
stdout.log:
This is message n. 0 on the standard output
This is message n. 1 on the standard output
This is message n. 2 on the standard output
stderr.log:
05/20/10 15:18:52 - This is message n. 0 on the standard error
05/20/10 15:18:53 - This is message n. 1 on the standard error
05/20/10 15:18:54 - This is message n. 2 on the standard error