On Mon, Jan 5, 2015 at 3:20 PM, Harry Putnam <rea...@newsguy.com> wrote:
> When running  shell commands from perl script with an open() instead of
> system() or exec how is the return value snagged.
>
> I guess, if it doesn't die, it worked but how to snag that information
> and at what point?
>
> ------- 8< snip ---------- 8< snip ---------- 8<snip -------
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $log = '/home/harry/some.log';
> my $cmd = 'rsnapshot -c rsnap_Home.conf hourly';
>
> open my $fh, '>', "$log" or die
>   "Can't open $log: $!";
>
> open my $ch, '-|', "$cmd" or die
>   "Can't run $cmd: $!";
>
> while (<$ch>) {
>   print;
>   print $fh;
> }
>
> close $fh;
> close $ch;
>

By "snag that information" , do you mean the shell program's output ?
Did the 'open' appear to succeed but without generating any output
from your read loop? Does the read loop hang?

If not,  checking the close status as previously shown may reveal the
problem. But if your read loop hangs and there's no output,  the shell
process itself may be hanging for
some reason.   IPC::Open3 or the more versatile IPC::Run  might provide some
info about what was going on prior to the hang if the info isn't buffered:

use IPC::Run qw/run/;

my @cmd =("rsnapshot....");
run( \@cmd,  \undef, \$out, \$err) or die "run: $!";
say "out:$out \nerr:$err"'

-- 
Charles DeRykus

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to