1832339 Members
2613 Online
110041 Solutions
New Discussion

Re: File loging

 
hpuxrox
Respected Contributor

File loging

Hi,

I am looking for a way to log stdin and stderr inline from within a script and send the information to logfile and terminal without the use of the "tee" command.

ex:

exec 2>&1 1> logfile.log
14 REPLIES 14
James R. Ferguson
Acclaimed Contributor

Re: File loging

Hi:

#!/usr/bin/sh
exec 1> /tmp/mylog
exec 2>&1
echo "stdout to log..."
print -u2 "stderr to log, too"

Regards!

...JRF...
Jeff_Traigle
Honored Contributor

Re: File loging

You can't. That's why tee exists.
--
Jeff Traigle
hpuxrox
Respected Contributor

Re: File loging

Ok, it I must use tee how would I do that inline?
A. Clay Stephenson
Acclaimed Contributor

Re: File loging

You were already very close:

ls 2>&1 | tee /var/tmp/outfile
If it ain't broke, I can fix that.
Court Campbell
Honored Contributor

Re: File loging

exec 2>&1 | tee logfile.log
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Jeff_Traigle
Honored Contributor

Re: File loging

your command | tee -a logfile

See the tee(1) man page. (There's not much more to it than that.

You can send the stdout and stderr to the log for the entire script as follows:

#!/usr/bin/sh
(
your command in here
) | tee -a logfile

The parentheses start a subshell from which all stdout and stderr is piped to the tee command. Much easier than adding the tee command to every command that generates output in the script.
--
Jeff Traigle
hpuxrox
Respected Contributor

Re: File loging

by inline, I mean from within the script, not from one line.

#!/usr/bin/sh
exec 1> /tmp/mylog
exec 2>&1
echo "stdout to log..."
print -u2 "stderr to log, too"

basically the above but needs to go to the terminal too..
Court Campbell
Honored Contributor

Re: File loging

You might try:

#!/usr/bin/sh
exec 1> /tmp/mylog | tee /dev/console
exec 2>&1
echo "stdout to log..."
print -u2 "stderr to log, too"
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
A. Clay Stephenson
Acclaimed Contributor

Re: File loging

Okay, I thought you would be able to take it from the point that I left it, but ...

Create a file my.sh:
----------------------------------
#!/usr/bin/sh

exec 2>&1
some_command
echo "Test Stuff"
some_opther_command
exit 0
-----------------------------------

Now when you invoke my.sh from an interactive shell or from a wrapper shell:

my.sh | tee /var/tmp/outfile

All stderr and stdout from my.sh will de directed to both the controlling terminal and to /var/tmp/outfile.
If it ain't broke, I can fix that.
hpuxrox
Respected Contributor

Re: File loging

Thanks clay, but all must be in one script. I can not have sub-scripts. Don't ask me why.. long story.
Peter Nikitka
Honored Contributor

Re: File loging

Hi,

I hope you can call your script more than once:

#!/usr/bin/ksh
if [ -z "$MY_SCRIPT_WRAPPER" ]
then
export MY_SCRIPT_WRAPPER=1
exec $0 2>&1 | tee /tmp/logfile
exit 0
fi

# original code here

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
hpuxrox
Respected Contributor

Re: File loging

After hacking around, I came up with this,

#!/usr/bin/sh
NAMEDPIPE=/tmp/TDL.pipefile
mkfifo ${NAMEDPIPE}
cat ${NAMEDPIPE} | tee sample.log &
exec 1> ${NAMEDPIPE}
exec 2>&1


#Main Script
ps -ef
print -u2 "stderr to log, too"

Seems to work, anyone have any better ways?

A. Clay Stephenson
Acclaimed Contributor

Re: File loging

Here's another approach:

Have your script build a script "on the fly" either with echo's or a heredoc section and then invoke the built script and have a trap to remove the temp script file on exit. This meet's your requirement of having it all in one script. This is a common technique for doing complex operations in the shell or if you have to build awk scripts on the fly and then invoke them.
If it ain't broke, I can fix that.
Peter Nikitka
Honored Contributor

Re: File loging

Hi,

I would append something unique to the name of the pipe like
NAMEDPIPE=/tmp/TDL.pipefile.$$

Multiple instances of your script running at the same time would cause trouble otherwise.

Removing the temporary FIFO after the run of the script will prevent potential permission problems, when at later times an existing FIFO created by another user is found at runtime.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"