R
Rob Baxter
I'm trying to implement a java program which will mirror the
functionality of the Unix utility tail -f (ie continually read the end
of a file). I have a fairly simple solution using FileInputStream &
bufferedReader in an infinite loop where I call Thread.sleep(1000)
every time the read returns null (indicating the end of the
FileInputStream has been reached).
The problem I am having is that I am using this to read the end of a
syslog log file and the files are logrotated every hour. When the
logrotate process renames the file I have opened my program continues
to read from the same file which is no longer being written to by
syslog. What I am looking for is a way to either be notified or be
able to determine when another process has moved/renamed a file I am
reading from. I've tried getting the file descriptor from the
FileInputStream and calling it's valid() method but it always returns
true even if I delete the file after it has been opened.
I know I can rig something up using the filesize to check if the file
has been rotated, but I was wondering if there was a more elegant
solution. any hints or advice are greatly appreciated (also if anyone
knows of a version of tail which doesn't suffer from this same problem
I'd be interested in seeing that also)
TIA,
-rob
functionality of the Unix utility tail -f (ie continually read the end
of a file). I have a fairly simple solution using FileInputStream &
bufferedReader in an infinite loop where I call Thread.sleep(1000)
every time the read returns null (indicating the end of the
FileInputStream has been reached).
The problem I am having is that I am using this to read the end of a
syslog log file and the files are logrotated every hour. When the
logrotate process renames the file I have opened my program continues
to read from the same file which is no longer being written to by
syslog. What I am looking for is a way to either be notified or be
able to determine when another process has moved/renamed a file I am
reading from. I've tried getting the file descriptor from the
FileInputStream and calling it's valid() method but it always returns
true even if I delete the file after it has been opened.
I know I can rig something up using the filesize to check if the file
has been rotated, but I was wondering if there was a more elegant
solution. any hints or advice are greatly appreciated (also if anyone
knows of a version of tail which doesn't suffer from this same problem
I'd be interested in seeing that also)
TIA,
-rob