mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Is it possible to capture STDOUT (*And STDERR if possible) when using the Proc::Background Module?

I did a good bit of searching for this and the only thing I really found that might have made sense was to do
something like this below, but I get errors when I do. The stuff I found from some of the searches I did was the part
at the end of the code where I'm opening a FILEHANDLE... But I get errors, when I do that.

       *F.Y.I.: This is part of a Gtk2 GUI I'm writing, and the code is from inside a function that executes when you click a button...
--------------------------------------------------------------------- my $proc = Proc::Background->new($bg_script, "$ARG1", "$ARG2"); my $PID = $proc->pid; # Get the PID my $start_time = $proc->start_time; # Get the Start Time # Check if the Job is still alive... 1==YES, 0==NO my $alive = $proc->alive; # While $alive is NOT '0', the process is still running while($alive ne 0) { # Check again if it's still running... $alive = $proc->alive; # This while loop will force Perl::Gtk2 to continue # processing GUI Events while BG process continues executing... while (Gtk2->events_pending) { Gtk2->main_iteration; } Gtk2::Gdk->flush; # Sleep for 1000 milliseconds usleep(1000); } # Capture the Exit Status from Background Script my $socket_RETCODE = $proc->wait; # Divide RETCODE by 256 to get actual RETCODE $socket_RETCODE /= 256; ########################################################### ### Attempt to Capture the OUTPUT: open( PROC, "$proc" ) or die "Can't read from $proc: $!\n"; my $output = print PROC; ########################################################### ---------------------------------------------------------------------
OUTPUT ERRORS FROM 'open()' COMMAND:
*** unhandled exception in callback:
*** Can't read from Proc::Background=HASH(0x8a2a2ac): No such file or directory


So I'm assuming from the errors above that you'd have to do that 'open()' command while the Background Job is still running..??

And if I move the open() cmd to just before the while loop and then put the assignment statement for $output variable inside the
while loop It will just hang at that point and I am forced to have to Ctrl+C to kill it...

I figure this is somehow possible I just can't figure it out...
Any hints/suggestions would be GREATLY appreciated..!!


EDIT: After reading some more on the open() command for this purpose I'm realizing they were using the open command to START
the job and not just to capture the output...

Thanks in Advance,
Matt

Replies are listed 'Best First'.
Re: Capturing STDOUT and/or STDERR from Proc::Background
by Anonymous Monk on Aug 09, 2013 at 03:07 UTC
      Hey Anonymous Monk, thanks for the reply!

      Ok, cool I'll check out those links. Didn't think to check the cache, or to even add something to the background script
      that could write out to a file instead of just STDOUT... Wasn't sure if I had mentioned that the background process is also
      a script I wrote, so adding something to do the redirecting/capturing sounds like a plan...!

      Great Idea...! Thanks!


      Thanks Again,
      Matt

        I meant Capture::Tiny, along the lines of

        ... my $session = get_session_id(); my $cache = get_cache_handle(); $cache->set( $session, [ 0, "" ] ); # no data yet my $background = go_background( $session ); ... if( not $background->alive ) { if( my $data = $cache->get( $session ) ){ return show_results( $data ); } } ... sub get_cache_handle { use Cache::FileCache; return Cache::FileCache->new ... }

        ## stuffintocache.pl #!/usr/bin/perl -- use strict; use warnings; use Getopt::Long(); use Capture::Tiny qw/ capture /; Main( @ARGV ); exit( 0 ); sub Main { my %opt; Getopt::Long::GetOptionsFromArray( \@_, \%opt, 'session=s', ... ) my($stdout, $stderr, $exit) = capture { system( $cmd, @args ); }; my $session = get_session_id(); my $cache = get_cache_handle(); $cache->set( $session, [ 1, $stdout, $stderr, $exit ] ); return; }