簡體   English   中英

IPC :: open3錯誤處理,沒有選擇

[英]IPC::open3 error handling with out select

下面的代碼工作正常,但是我想在不使用IO :: Select的情況下實現相同的功能-我嘗試了很少的事情,但是沒有任何效果,因為我對perl不太熟悉。

sub writeTologs
{
my $cmd=shift;

open(ERRLOG, '>>log//error.log') or die "Can't open error log! $!";
open(OUTPUT, '>>log//output.log') or die "Can't open output log! $!";

my ($infh,$outfh,$errfh); # these are the FHs for our child
$errfh = gensym(); # we create a symbol for the errfh   # because open3 will not do that for us

my $pid;
               #
eval{
$pid = open3($infh, $outfh, $errfh, $cmd);
};
die "open3: $@\n" if $@;
               #
print "PID was $pid\n";


my $sel = new IO::Select; # create a select object to notify
                      # us on reads on our FHs (File Handlers)
$sel->add($outfh,$errfh); # add the FHs we're interested in
                      #
    while(my @ready = $sel->can_read) { # read ready
            foreach my $fh (@ready) {
                    my $line = <$fh>; # read one line from this fh
                    if(not defined $line){ # EOF on this FH
                    $sel->remove($fh); # remove it from the list
                    next;              # and go handle the next FH
                    }

                    if($fh == $outfh) {     # if we read from the outfh
                            print OUTPUT $line; # print it to OUTFH
                    }
                    elsif($fh == $errfh) {# do the same for errfh
                            print ERRLOG $line;
                            die "Error found : $@";
                            }
                            else { # we read from something else?!?!
                                    die "Shouldn't be here\n";
                            }
                    }
    }

close(ERRLOG) or die "Can't close filehandle! $!";
close(OUTPUT) or die "Can't close filehandle! $!";

 }

我想在寫入ERRLOG后終止進程。

我試過下面的代碼-

open(ERRLOG, '>>log//error.log') or die "Can't open error log! $!";
open(OUTPUT, '>>log//output.log') or die "Can't open output log! $!";

my ($infh,$outfh,$errfh); # these are the FHs for our child
$errfh = gensym(); # we create a symbol for the errfh   # because open3 will not do that for us

my $pid;
my $line="";                   #
eval{
$pid=open3($infh, $outfh, $errfh, $cmd1);
};
die "open3: $@\n" if $@;

if (defined(<$errfh>)) {
        print "Error start ".<$errfh>;
        $errfh->close;
        while($line = <$errfh>) {
                print $line;
                print "Inside while";
        }

        print "Error deifned \n";
} else {

        while ($line=<$outfh>) {
                print OUTPUT $line;
        }
        print "Output log";
}

但是while循環不會在if條件內執行。

我能夠從下面的代碼中實現這一點-

sub writeTologs {
my $cmd      = shift;
my $strout   = "$LOGDIR/$STDLOG";
my $strerr   = "$LOGDIR/$ERRORLOG";
my $flag     = 0;
my $line     = '';
my $pid;

open(ERRLOG   , '>>', $strerr  ) or die "Can't open error log! $!";
open(SCHDLOG , '>>', $strout  ) or die "Can't open output log! $!";

my ($infh,$outfh,$errfh);
$errfh = gensym();

eval
{
    $pid = open3($infh, $outfh, $errfh, $cmd);
};
die "Open faile : $@" if $@;

my $line_err  = <$errfh>;

if (defined(<$outfh>))
{
    while($line = <$outfh>)
    {
            print SCHDLOG '[' . get_timestamp() . "]: $line";
    }
}

if ($line_err)
{
    print ERRLOG $line_err;
    $flag =1 ;
}

print "PID = $pid";

waitpid($pid,0);

close(ERRLOG)  or die "Can't close filehandle! $!";
close(SCHDLOG) or die "Can't close filehandle! $!";
return $flag;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM