1821470 Members
2793 Online
109633 Solutions
New Discussion юеВ

stdout redirect

 
Ryan Ma
Frequent Advisor

stdout redirect

How can I redirect the stdout?

For example if I have direct the stdout to a file, can I redirect the stdout to a terminal later?
17 REPLIES 17
Robin Wakefield
Honored Contributor

Re: stdout redirect

Hi Ryan,

If you are saying that you redirected stdout by typing:

exec 1>filename

then to get it back to redirecting to the terminal, just type:

exec 1>/dev/tty

Rgds, Robin.
Bill McNAMARA_1
Honored Contributor

Re: stdout redirect

you should be able to cat it to the tty (or redirect) once

ie
pereal:root> who am i
root ttyp2 Jan 15 09:54
pereal:root> hello
---

pereal:root> who am i
root ttyp1 Jan 15 09:54
pereal:root>echo "hello" > /dev/ttyp2


pereal:root> ll /dev/ttyp2
cr-------- 2 root tty 17 0x000002 Jan 15 09:56 /dev/ttyp2

even when
pereal:root> mesg
is n

Later,
Bill
It works for me (tm)
Printaporn_1
Esteemed Contributor

Re: stdout redirect

if you want to redirect output to both terminal and file

#command | tee filename
like

#ll | tee file_list
enjoy any little thing in my life
Ryan Ma
Frequent Advisor

Re: stdout redirect

I have to make it clear.
Please read the example.

# backup > backup.log &

After running the command, can I make the stdout to other places such as tty when I have already assigned it to backup.log before and vice versa.
A. Clay Stephenson
Acclaimed Contributor

Re: stdout redirect

Hi:

If I understand your question, you don't need to do anything.

backup > backup.log &
ls
echo "Test"

In this example, only stdout from the command backup will be redirected to stdout. The output
from the commands ls and echo "Test" will then appear on your terminal (assuming that your terminal is stdout). Notice also that since you dropped backup into the background that the next two commands execute immediately. If you are trying something like:

backup > backup.log &
backup2 > backup2.log &
backup3 > backup.log &
wait
echo "Test"

Then all three of the background commands must complete before wait allows the echo statement to execute.

Regards, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: stdout redirect

Oops ....

I should have said:
In this example, only stdout from the command backup will be redirected to the file 'backup.log'.
If it ain't broke, I can fix that.
Ryan Ma
Frequent Advisor

Re: stdout redirect

In fact, when the following command is run.

# backup > backup.log

The stdout is redirected to backup.log

And now I want this existing redirection back to terminal (tty) again.

Can I do that?
A. Clay Stephenson
Acclaimed Contributor

Re: stdout redirect

Hi Ryan:

I'll try again because I don't think I understand your question.

backup > backup.log

In this case only the stdout from the command 'backup' is sent to the file backup.log but your terminal seems hung because it must wait for the command to complete. As soon as backup has finished, output to your terminal then resumes normally.

If you want to then send the contents of 'backup.log' to your terminal then that is nothing more than 'cat backup.log'. I think the best way to determine what you are doing is to experiment with a diffrent command that doesn't take so long to complete. I suggest that you temporarily substitute something like
'ls /tmp > backup.log' for you backup command just so you can get an idea of how to proceed.

Clay
If it ain't broke, I can fix that.
Ryan Ma
Frequent Advisor

Re: stdout redirect

Sorry I have not typed & at the end of command.

It seems there is no way to do that and I must do this

# tail -f backup.log

I just want to make the stdout back to tty after I have submitted the command

# backup > backup.log &

so that the result as if I have submitted this command

# backup &
Ruediger Noack
Valued Contributor

Re: stdout redirect

Hi,

I think if I you understand, you have to use the tee command like this:
backup | tee backup.log or
backup | tee backup.log &

Now your output is redirected to backup.log and also to tty.

I use often (for this example):
backup 2>&1 | tee backup.log

Hope this helps

Ruediger
Ryan Ma
Frequent Advisor

Re: stdout redirect

Using command tee can reserve the stdout to tty and at the same time write it to a file.

But I want it first redirect to a file, then later I want it back to tty.

Or can I transfer the stdout to another tty?
Ruediger Noack
Valued Contributor

Re: stdout redirect

Ryan,

try this:

backup | tee backup.log >

Ruediger
Steven Sim Kok Leong
Honored Contributor

Re: stdout redirect

Hi,

>> # backup > backup.log &

Note that if you want all errors to be recorded as well, you should log the STDERR as well:

# backup > backup.log 2>&1 &

>> But I want it first redirect to a file, then later I want it back to tty.
>> Or can I transfer the stdout to another tty?

To direct it back to the tty, run (as what Bill has mentioned):

# cat backup.log > /dev/ttyp0

If you want another tty say /dev/ttyp2, then:

# cat backup.log > /dev/ttyp2

If you want it to be displayed on your current login tty, then:

# cat backup.log

Hope this helps. Regards.

Steven Sim Kok Leong
Ryan Ma
Frequent Advisor

Re: stdout redirect

I usually think that there is a way to "redirect the stdout back to tty after you redirect it to a file". If I success bringing it back, then I can do something interactively such as press Ctrl-C to stop it.

*********************************

If you just "cat" the log file, then you are submitting another command to view the log file rather than re-redirect the stdout back to tty.

Also, if you use "cat", then you will not get further information from the log file since it may be kept updating. So using "tail -f" may be more appopriate.

*********************************

May be I am just thinking something that doesn't exist.

Thanks for all of you discussing this.
Steven Sim Kok Leong
Honored Contributor

Re: stdout redirect

Hi,

Now I think I get what you mean. You want to pass control from a background process back to the foreground while being able to view the output of the process, yet log this output to a file.

Correct me if I am wrong.

In that case, then you should execute:

# backup | tee backup.log & # this will display output on the screen, log to a file while the backup continues running.

# fg # this will bring the backup process back up in the foreground for you to execute a ctrl-c to abort the backup.

Hope this helps. Regards.

Steven Sim Kok Leong
Ryan Ma
Frequent Advisor

Re: stdout redirect

Yup.

I want to transfer the stdout to a file or other tty.

And if another tty get the stdout, the result should be as if it is originally submitted from that tty.
Ruediger Noack
Valued Contributor

Re: stdout redirect

Hi Ryan,

your last answer confuses me.
You want to start and control your job from first tty and you need a additional output to a file or another tty. The output has to be the same. Isn't ??t?
And the job has to run in background or not.
If is so, I don't see the problem.

Ruediger