tags:

views:

76

answers:

3

Hello friends,

Let's say I have the following simple script:

print "ID : ";
$ID = <>;
system (`comp program $ID`);
exec "task --shell";

When I use:

perl foo.pl | tee log.txt

The showing up problem is getting on screen a blink sign echo (waiting for the ID enter) before I even see the "ID : " (print instruction).

I need to keep on a log file all running output script (very long), notice that at the start of the run I have an interactive part that also need to be kept.

Is there a way inside PERL script or outside it, that keep the output stream exactly as it shown out on the screen, and what do you think is the simple & efficient way to conduct it?

I've noticed the IO::Tee , File::Tee moudles and Log4perl - someone can help me find the best way to use it ?

A: 

You could run everything inside a screen session with logging enabled.

innaM
+3  A: 

The best approach I have seen is to use script program.

You run script (program) - it gives you shell. in this shell you run anything you want. When you're done, you exit this shell, and everything that was on your screen will be in typescript file. Including all control codes.

There is also "bonus" - you can replay saved session with scriptreplay.

depesz
Thank you depesz , definitely it is a useful tool! thank you for informing me. When I use the arrow short cut, it also capture the previous commands with dump signs.
YoDar
+1  A: 

It's possible that your script is asking for user input before printing the "ID: " prompt because you don't have autoflush turned on. Try adding the following:

$|++;
print "ID : ";
$ID = <>;
system (`comp program $ID`);
exec "task --shell";

If this works then you might look at the Perl documentation about output buffering. perldoc -q buffer will explain what's happening in more detail as well as show some alternate ways of turning on autoflush.

If this doesn't solve the problem then it's possible that the tee command is buffering your output. In this case I would use script or screen as suggested by depesz or Manni

benrifkah
Thanks benrikah, Thats seems to capture "ID : " before input entering, but now the missing part is the input itself. I'll check perldoc -q buffer as you said. thank you for the answer!
YoDar
@YoDar You've got two issues here. The first is that you weren't seeing the output of your script in "realtime". The second issue is that you need to capture the output of your script along with user input. For that I would definitely use an external capturing program like script or screen.
benrifkah
@YoDar Also, while my answer solves the buffering issue it seems like one of the other responses form depesz or Manni will actually, give you a better overall solution. Capturing the output via one of these methods will bypass the command pipeline and perl will no longer buffer it's output.
benrifkah