Redirect output to console and to file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vasanthan111
    New Member
    • May 2008
    • 1

    Redirect output to console and to file

    Hi friends,

    Kindly help me regarding the issue Im facing with the below perl
    script,

    I have a below script which send the message to Console.

    I need to modify this script it send message to a Log file.

    Please find the below steps which i performed to send a message to
    log.

    Orginal Script:
    ===============
    Code:
    return -1 on error
    #        0 pkg is starting or running and switchable
    #        1 pkg is starting or running and not switchable
    #        2 pkg is not running
    _show_pkg() {
       local pkg=${1:-ALL}        #i.e 'GENPKG1,GENPKG3' or 'ALL' or
    'GENPKG1' or ...
       local mode=${2:-silent}    #i.e 'normal'
    
       clustat -flx | perl -MXML::Twig -e'
           my %S = (started => q/UP/, recoverable => q/FAILED/, pending
    => q/FAILED/, stopped => q/DOWN/, failed => q/FAILED/, disabled => q/
    DOWN/ );
           my @A = split /,/, $ARGV[0];
           my ($ret,$cs) = (0,0);
           my $twig = new XML::Twig(TwigRoots => {node => 1, group => 1},
                                    TwigHandlers => { node  => sub {
    my ($t,$n) = @_;
    $cs = $ret = 1 unless ($n->att(rgmanager) eq q/1/ and $n->att(state)
    eq q/1/);
    },
                                                      group => sub {
    my ($t,$g) = @_;
    my $pkg = (split /:/,$g->att(name))[1];
    return unless (q/ALL/ eq $A[0] or grep /$pkg/, @A);
    my $owner = "on ".substr($g->att(owner),0,-2) if $g->att(state_str) eq
    q/started/;
    unless ($ARGV[1] eq q/silent/) {
      print("State of $pkg is $S{$g->att(state_str)}");
      print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
    switchable") if $owner;
      print "\n";
    }
    $ret = 1 if $g->att(restarts)>0;
    $ret = 2 unless $g->att(state_str) eq q/started/;
    }});
           print(STDERR "Cannot open XML::Twig\n"), exit -1 unless
    defined $twig; $twig->safe_parse(STDIN); exit $ret;
    ' ${pkg} ${mode}
    }
    Note ===> Steps

    1. The output of the above script will display below information in
    Screen ( console), it
    will only display GENPKG{1..5} information on screen.

    [root@incciozr08 46 hbha]# show_pkg
    State of GENPKG1 is FAILED
    State of GENPKG2 is FAILED
    State of GENPKG3 is FAILED
    State of GENPKG4 is FAILED
    State of GENPKG5 is FAILED
    [root@incciozr08 46 hbha]#

    2. I want to send the same message to log file, So I adopted the
    script as shown below.
    Code:
    my $file1 = "/tmp/pmon/usbs_manage_ha.log";
    open FILE, ">>$file1" or die "unable to open $file1 $!";
    
    unless ($ARGV[1] eq q/silent/) {
      print("State of $pkg is $S{$g->att(state_str)}");
      print FILE ("State of $pkg is $S{$g->att(state_str)} \n ");
      print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
    switchable") if $owner ;
      print FILE (" $owner and ",($g->att(restarts)>0 or $cs)?"not":"
    "," switchable \n" ) if $owner ;
      print "\n";
    }
     close FILE or die;
    3) Kindly note, Now im able to write to $file1 ("/tmp/pmon/
    usbs_manage_ha. log"), but its continiously writing to the
    $file1 in loop,

    < extract of messgae in log >


    #*#*#*#*#*#*#*# *#*#*#*#*#*#*#* #*#*#*#*#*#*#*# *#*#*#*#*#*#*#* #*#*#*#*#*#*#*# *#*#*
    #H: show_pkg called by: bash[2208] at Sat Jan 19 03:46:41 CET 2002
    State of GENPKG1 is FAILED
    State of GENPKG2 is FAILED
    State of GENPKG3 is FAILED
    State of GENPKG4 is FAILED
    State of GENPKG5 is FAILED
    #H: show_pkg (ret = 2) terminate at Sat Jan 19 03:46:41 CET 2002
    State of GENPKG5 is FAILED
    State of GENPKG1 is FAILED
    State of GENPKG2 is FAILED
    State of GENPKG3 is FAILED
    State of GENPKG4 is FAILED
    State of GENPKG5 is FAILED
    State of GENPKG1 is FAILED
    State of GENPKG2 is FAILED
    State of GENPKG3 is FAILED
    State of GENPKG4 is FAILED
    State of GENPKG5 is FAILED
    State of GENPKG1 is FAILED
    State of GENPKG2 is FAILED
    State of GENPKG3 is FAILED


    4) Please provide your inputs to get rid of this loop
    Last edited by numberwhun; Jan 14 '11, 05:27 PM. Reason: Please use code tags!
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    PerlIO::tee - Multiplex output layer

    Comment

    Working...