I need to execute a command from my Perl script, which will take some time (1-2 hours). I want to see the output of the command, so that my script can verify that everything went fine, but since it takes so long, I would like the user to see the commands that are displayed during operation, too.
What I tried:
backticks - can get the result only after the completion of the commandsystem - can get the result only after completion of the commandopen - Almost perfect - but the output of the commands is buffered, that is, users do not see the update for a long time. The Internet is full of offers to install $| = 1 $| = 1 , but apparently this only affects input buffering and does not work.- Piping to
tee - Similar open results - 'tee' seems to print later - Re-redirecting the output and using
Proc::Background and File::Tail is almost perfect again, but can't come up with an elegant way to stop the print cycle
I would like to have an offer!
Edit: I accepted Barmars answer. I believe this works because Expect.pm uses a pseudo terminal. Just for others considering this issue in the future, here's how I implemented it:
my $process = Expect->spawn($command, @params) or die "Cannot spawn $command: $!\n"; while ($process->expect(undef)) { print $process->before(); }
source share