473,414 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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:
===============
Expand|Select|Wrap|Line Numbers
  1. return -1 on error
  2. #        0 pkg is starting or running and switchable
  3. #        1 pkg is starting or running and not switchable
  4. #        2 pkg is not running
  5. _show_pkg() {
  6.    local pkg=${1:-ALL}        #i.e 'GENPKG1,GENPKG3' or 'ALL' or
  7. 'GENPKG1' or ...
  8.    local mode=${2:-silent}    #i.e 'normal'
  9.  
  10.    clustat -flx | perl -MXML::Twig -e'
  11.        my %S = (started => q/UP/, recoverable => q/FAILED/, pending
  12. => q/FAILED/, stopped => q/DOWN/, failed => q/FAILED/, disabled => q/
  13. DOWN/ );
  14.        my @A = split /,/, $ARGV[0];
  15.        my ($ret,$cs) = (0,0);
  16.        my $twig = new XML::Twig(TwigRoots => {node => 1, group => 1},
  17.                                 TwigHandlers => { node  => sub {
  18. my ($t,$n) = @_;
  19. $cs = $ret = 1 unless ($n->att(rgmanager) eq q/1/ and $n->att(state)
  20. eq q/1/);
  21. },
  22.                                                   group => sub {
  23. my ($t,$g) = @_;
  24. my $pkg = (split /:/,$g->att(name))[1];
  25. return unless (q/ALL/ eq $A[0] or grep /$pkg/, @A);
  26. my $owner = "on ".substr($g->att(owner),0,-2) if $g->att(state_str) eq
  27. q/started/;
  28. unless ($ARGV[1] eq q/silent/) {
  29.   print("State of $pkg is $S{$g->att(state_str)}");
  30.   print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
  31. switchable") if $owner;
  32.   print "\n";
  33. }
  34. $ret = 1 if $g->att(restarts)>0;
  35. $ret = 2 unless $g->att(state_str) eq q/started/;
  36. }});
  37.        print(STDERR "Cannot open XML::Twig\n"), exit -1 unless
  38. defined $twig; $twig->safe_parse(STDIN); exit $ret;
  39. ' ${pkg} ${mode}
  40. }
  41.  
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@incciozr0846 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@incciozr0846 hbha]#

2. I want to send the same message to log file, So I adopted the
script as shown below.
Expand|Select|Wrap|Line Numbers
  1. my $file1 = "/tmp/pmon/usbs_manage_ha.log";
  2. open FILE, ">>$file1" or die "unable to open $file1 $!";
  3.  
  4. unless ($ARGV[1] eq q/silent/) {
  5.   print("State of $pkg is $S{$g->att(state_str)}");
  6.   print FILE ("State of $pkg is $S{$g->att(state_str)} \n ");
  7.   print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
  8. switchable") if $owner ;
  9.   print FILE (" $owner and ",($g->att(restarts)>0 or $cs)?"not":"
  10. "," switchable \n" ) if $owner ;
  11.   print "\n";
  12. }
  13.  close FILE or die;
  14.  
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
Jan 13 '11 #1
1 4223
RonB
589 Expert Mod 512MB
PerlIO::tee - Multiplex output layer
http://search.cpan.org/~gfuji/PerlIO.../PerlIO/tee.pm
Jan 13 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: andy.t.chou | last post by:
I need to be able to have a hidden field (signiture) with each output html file generated. # (Get the first and last name from the html form) $first_name = $q->param('FirstN'); $last_name =...
2
by: Oleg Ogurok | last post by:
Hi all, On my web page, I have a <OBJECT DATA="mydatafile.ica" ...> which loads Citrix ICA web client. The problem is that I want to generate the mydatafile.ica on the fly (e.g. based on...
2
by: Jason | last post by:
Hello, I have a class, transCore, that does certain work, and by default, prints its progress to the stand output. Later, I will to write a GUI class that encapsulate the transCore class. I...
0
by: Christophe HELFER | last post by:
Hi, I've post some question last week about redirecting input and output console. Then I've received some example code from H.K. Wagner to see how redirecting works. This example works fine...
2
by: 28tommy | last post by:
Hi there, I'm doing some TELNET and FTP sessions with my scripts. I need to redirect all the output (and not just what I print) to an output file, but still be able to see the session in process...
7
by: Nananana | last post by:
Hi. I have 2 DB2 statements: connect select How can I create a batch file and redirect the output to a file? I would like something like this:
1
by: mquincey | last post by:
One of the features offered by .NET 2.0 is the use of the TraceSource class. In an attempt to demonstrate its use, I wanted to run my test under the following conditions: 1. Use TraceSource class...
4
by: standerby | last post by:
I am writing a XML file suing XElement. How can I output a file like the following? I got an exception if if I use ':' in the element name or attribute name. Thanks, <?xml version="1.0"...
2
by: ashokd001 | last post by:
Hi , How do i redirect output of topas to a file. i did not find any such option in topas command. Regards, Ashok
2
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.