Hi friends
I need to monitor a file which is modified or not. if modified i need to get a mail mail or error message.
Eg: monitor.cfg file
Hi friends
I need to monitor a file which is modified or not. if modified i need to get a mail mail or error message.
Eg: monitor.cfg file
you can use tools like md5sum, cksum, sha1 etc to monitor your file. an alternative in Perl
#!/usr/bin/perl
use Digest::MD5 ;
my $file2monitor = "file";
sub getmd5 {
my $file = $_[0];
open(FH,"<",$file) or die "Cannot open file: $!\n";
binmode(FH);
my $md5 = Digest::MD5->new;
return $md5->addfile(*FH)->hexdigest;
close(FH);
}
my $original_sum = getmd5 $file2monitor;
while (1){
my $getcksum = getmd5 $file2monitor;
if ( $getcksum ne $original_sum ){
print "File $file2monitor changed:\n";
}
sleep 3;
}