1845960 Members
2206 Online
110250 Solutions
New Discussion

Re: Message redirection

 
Chakravarthi
Trusted Contributor

Message redirection

Hi all,

I'm running an application, it prints lot of printf statements on the terminal, i want to redirect all of the messages to a log file,

i tried doing this using "command > lofile 2>&1", this doesnt work, but the same command works for all unix commands. Is there any other way to capture the messages


regards
chakri
7 REPLIES 7
Chris Wilshaw
Honored Contributor

Re: Message redirection

You can do either

command | tee -a logfile

or

script logfile
command


The script option may be preferable, as this also captures any user keystrokes involved during the process.
Chuck J
Valued Contributor

Re: Message redirection

You can do:

# script

when you've finished you do a CTRL+D to end the log file. You can then cat, more, print or do whatever with the logfile.

Chuck J
Chakravarthi
Trusted Contributor

Re: Message redirection

sorry tee is not working
Chakravarthi
Trusted Contributor

Re: Message redirection

Hi,

tee is also working,, the program is a C program, with lot of printf statements, script works, but the problem with script is i can see the log only after closing the program,

Is there anyway i can get the log messages without closing the application

regards
chakri
Chuck J
Valued Contributor

Re: Message redirection

Open a 2nd window and do a tail -f on your log file:

# tail -f logfile

This will update whenever the logfile gets written to.

Don't forget to assign us points please :-)

Chuck J
Chakravarthi
Trusted Contributor

Re: Message redirection

I'm trying this on linux machine, it is not saving the messages until i close the session,,,

Darrell Allen
Honored Contributor

Re: Message redirection

Hi,

Here's a guess as to why "this doesnt work, but the same command works for all unix commands".

The application has redirected stdout to the tty device, effectively over-riding what you are attempting with "command >lofile 2>&1".

Check with the application developer to see if that's the case.


Consider the following simple test...

# cat script
#!/usr/bin/sh
echo first message
TTY=$(tty)
echo second message >$TTY

# ./script
first message
second message

# ll
total 2
-rwx------ 1 root sys 57 Nov 20 10:07 script

# ./script >out
second message

# ll
total 4
-rw-r--r-- 1 root sys 14 Nov 20 10:07 out
-rwx------ 1 root sys 57 Nov 20 10:07 script

# cat out
first message


Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)