LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-09-2014, 09:48 AM   #1
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 16

Rep: Reputation: Disabled
unable to write output of a fifo file to a log file


I am trying to read from a fifo file and append that along with the date to a log file.

I am able to write only the first line to the log "Starting the log".
But the write within the while loop after reading from the fifo doesnt happen.

It does only if I open the log file for writing within the while loop.
I want to be able to open the log for appending and write to it anywhere from the proceeding loops. Can someone please help fix the code.


#!/usr/bin/perl
use Path::Class;
#use autodie;

my $fifo = "pcode.pipe";
my $fifoh;
my $fifolog = "/nfs/home/kummit01/lab/fifo.log";

open($fifoh, "+< $fifo") or die "The FIFO file cant be found";

open (LOGH, "+>>$fifolog") or die "The Log file cant be opened";


print LOGH "Startintg the log\n";
while (<$fifoh>) {
chomp ($_);
$pattern = '^[A-Z]{1,2}[0-9]{1,2}?\s[0-9][A-Z][A-Z]$';
if ($_ =~ /$pattern/) {
#open (LOGH, ">>$fifolog") or die "The Log file cant be opened";
print "Match Success \n";
$timeNow = localtime(time);
print LOGH ("$_ $timeNow");
} else {
print "No Match \n";
}
}
close $fifoh;
close $logh;
exit (0);
 
Old 10-09-2014, 10:14 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
To ensure output is not buffered
Code:
$|=1;
 
Old 10-09-2014, 10:36 AM   #3
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
To ensure output is not buffered
Code:
$|=1;

Sorry but I am not clear with this.
Can you please show me where in the code to place this and how it works.

Thanks much.
 
Old 10-09-2014, 01:21 PM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
After any print/printf that you want written immediately.
 
Old 10-09-2014, 03:06 PM   #5
mithun119
LQ Newbie
 
Registered: Aug 2014
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
After any print/printf that you want written immediately.


I am still not able to do it.

Can you please modify part of the code and show how and where exactly to use it.
 
Old 10-10-2014, 06:55 AM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/usr/bin/perl -w

my $fifo = "pcode.pipe";
my $fifolog = "/nfs/home/kummit01/lab/fifo.log";
   $fifolog = "fifo.log";

open(F, "<$fifo") or die "The FIFO file cant be found";

open (LOGH, ">$fifolog") or die "The Log file cant be opened $!";
printf(LOGH "Starting the log\n");
$|=1;

while (<F>) {
 chomp($_);
 if (/^[A-Z]{1,2}[0-9]{1,2}?\s+[0-9][A-Z][A-Z]$/) {
   print "Match Success\n";
   $timeNow = localtime(time);
   printf(LOGH "%s - %s\n", $_, $timeNow);  $|=1;
 } else {
   print "No Match\n";
 }
}
exit (0);
If you've got stuck remove the pipe and make a new one.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Use SYSLOG to write log to new log file worm5252 Programming 2 08-23-2012 11:17 AM
write output of two commands to a file pgb205 Programming 1 10-12-2010 01:13 PM
After log roll, Postfix cannot write to log file, Ubuntu 8.04 mboudro Linux - Server 6 09-17-2010 03:10 AM
Clean Up Log - Search for Pattern in Log file and Output result bridrod Linux - Newbie 10 01-05-2010 09:49 AM
How to write output from expect to a file? johnpaulodonnell Programming 2 06-22-2007 05:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration