I
Ishmael
I would like to check a file continuously to see if something has been
written to it. If so, immediately run a script. I can do this
successfully by repeatedly polling the file, as shown below, but this
uses up lots of system resources. I can, of course, add a 'sleep' to
the while loop, which reduces the drag on the system, but it also
slows down the response time.
Can someone recommend a replacement for this code using some more
elegant means? Ideally, I would like to set some sort of interrupt as
soon as the \n character is written to the file. That way, I can
avoid doing any polling myself and let the system handle it. Maybe
select(2) or FIFOs would work, but I've read the documentation and
can't figure out if this is really what I want. Thanks for your help!
# ---- My naive way ----
# Listener
open(FID, ">myfile") || die("Can't open: $!");
while (1) {
if (my $line = <FID>) {
print "$line\n";
last;
}
}
# Writer
open(FID, ">myfile") || die("Can't open: $!");
print FID "test\n";
written to it. If so, immediately run a script. I can do this
successfully by repeatedly polling the file, as shown below, but this
uses up lots of system resources. I can, of course, add a 'sleep' to
the while loop, which reduces the drag on the system, but it also
slows down the response time.
Can someone recommend a replacement for this code using some more
elegant means? Ideally, I would like to set some sort of interrupt as
soon as the \n character is written to the file. That way, I can
avoid doing any polling myself and let the system handle it. Maybe
select(2) or FIFOs would work, but I've read the documentation and
can't figure out if this is really what I want. Thanks for your help!
# ---- My naive way ----
# Listener
open(FID, ">myfile") || die("Can't open: $!");
while (1) {
if (my $line = <FID>) {
print "$line\n";
last;
}
}
# Writer
open(FID, ">myfile") || die("Can't open: $!");
print FID "test\n";