If you want to write to two log files, you should combine this with tee:
Code:
${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:
Code:
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
Code:
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
Code:
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.
Code:
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
Code:
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.
Code:
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
Code:
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)
Code:
$ 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
Code:
$ 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
Hi All,
I am writing a shell script code. and i want the stderr to be send to a file and the stdout to be displayed in terminal. In my shell script code i use a read command to get data from user.read -r -p "Enter the type :" data
and while i execute my script i use./my_script.sh 2>... (4 Replies)
Hello,
Can you please if the bellow is the proper way of appending a variable to the stderr:
The easiest way to test this,I was able to imagine, was by touching 5 files and afterwards looping trough to the results:
-rw-r--r-- 1 ab owner 0 Sep 14 13:45 file1
-rw-r--r-- 1 ab owner 0 Sep... (7 Replies)
Hi Experts,
i have a solaris 9 OS and i get the following message repeated many time in my /var/adm/messages :
Oct 31 16:30:44 baobab rsh: can't get stderr port: Cannot assign requested address
have you any idea how can i resolve this issue ??:confused:
thanks for help (2 Replies)
Hello
I try to store stderr into a variable, then if this var is not empty i send an email and stop my script.
I think my problem is due of "<$dump" into my command line.
my bad command line (see samples below on this post)
if ! $returnedStr ;
then
echo ERROR READING DUMP: ... (8 Replies)
Can somebody explain to me why the diff output is not going to stderr?
Yet when I issue a diff from the command line the return code is -ne 1.
I am guessing diff always writes to stdout???
Is there away I can force the difff to write to stderr USING THE CURRENT
template. If possible, I... (5 Replies)
Hi there,
I was wondering if it was possible to pipe stderr to another process.
I need to eval commands given as arguments and I would like to redirect stderr to another process.
I can redirect stderr to a file like this...
toto:~$ command="one=1"
toto:~$ eval $command 2> error
toto:~$... (5 Replies)
Hi,
Need some help here on a script I'm writing. I know that STDERR is normally done is this manner:
script 2>stderr.out
However, if I wanted to output the stderr from a rsh command how do I do that?
Example:
su - username -c "rsh $hostname /opt/gilberteu/scriptname" 1>stdout... (5 Replies)
Herez the question,
In a process which writes into file FILE1 with descriptor
fHandler1 and it is run as a background process
where would statements be directed
when stderr descriptor is used.
fprintf(stderr,"some message\n");
assume that session from which it is run is terminated and... (3 Replies)