Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any ideas why this won't write to a file?? 1

Status
Not open for further replies.

jewel464g

Technical User
Jul 18, 2001
198
US
The script write to stdout, but won't write to a file. Does anyone know what's going on?? I have to have this done by Friday, so I would really appreciate ANY help. Thanks so much.

Jewel



#!/usr/bin/perl -w
BEGIN { unshift @INC,"./blib/lib/";}
use File::Tail 0.98;
$name="/var/log/snort/alert" unless $name=shift @ARGV;
$debug=shift @ARGV || 0;


#open the file for writing, append to whatever data may be there
open (file2, ">> /var/log/snort/alert2") || die "opening alert2: $!";

print "Looking at $name \n";

#
# This is the old, pure object, interface
#
$file=File::Tail->new(name=>$name,debug=>$debug,interval=>1,maxinterval=>5,
adjustafter=>20,errmode=>"return") or
die "Could not open $name: $!";

while ($line=$file->read) {
if ($Found) {
print $line;
print file2 $line;
$Found = 0;
next;
}
if($line =~ /Priority: 1/){
print $line;
print file2 $line;
$Found = 1;
}
}
 
The you need to give print the filehandle argument.

print file2 "hello";

 
Isn't that what I'm doing with this line?

print file2 $line;

 
Sorry didn't see that line. Well what you have should work assuming you are getting to that line. Is the file "/var/log/snort/alert2" being created at all. If the file is there, then the open worked. As a test, you could add a 'print file2 "hello";' line right after the open.

Good Luck
 
Yes it's creating it and it's opening it, But it just isn't writing to the file. I have already tried your suggestion with the test, I used print file2 "123"; and still nothing. I've used the same syntax in another program and it worked fine. Do you think the tail stuff is somehow messing it up??
 
Perhaps you need to force a flush?
Code:
$| = 1;
OR:
use FileHandle
autoflush OUTPUT 1;
Cheers, Neil :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top