Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Is a file currently being modified?

by njcodewarrior (Pilgrim)
on Aug 16, 2005 at 23:22 UTC ( #484275=perlquestion: print w/replies, xml ) Need Help??

njcodewarrior has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks,

The background to this current post is located in a previous node:

In the hopes of finding a simple solution to this problem, I'd like to write an application using a while (1) loop that periodically checks for either a new log file OR if a log file is currently being updated. A new log file is written each time an AUV calls in and is given a unique timestamp.

Checking for a new log file is easy (I think I'll use BrowserUK's solution). What I'd like to know is how to determine if a log file is currently being writen to and, if it is, how to read in and parse the new lines as they are being written?

Thanks in advance for your help.

Replies are listed 'Best First'.
Re: Is a file currently being modified?
by davido (Cardinal) on Aug 16, 2005 at 23:39 UTC

    Assuming other applications visiting this logfile are playing nice too, you could use flock with the LOCK_NB bitflag set (that is Lock Non-Blocking). This will cause flock to return immediately instead of waiting for a lock. When it returns, its status will tell you whether it was granted a lock or not. If not, you know something else has the file locked. Flocking is voluntary, so scripts/programs that don't lock the file or check to see if it's locked won't play nice with applications that do.

    As far as reading lines as they're being written, that's precarious. Due to buffering, you're unlikely to get line by line info, but rather chunks every so often.

    By the way, another option would be to periodically check last modification time, or file size. A change there will indicate the file has been written to.


Re: Is a file currently being modified?
by BrowserUk (Patriarch) on Aug 17, 2005 at 01:07 UTC

    Perhaps the easiest way would be to keep a record of how much of each file you have already read. Then your test for it having been written to, is just checking the current size of the file. A tied hash might be one way of persisting this information.

    Perhaps the harder thing is to determine when you've read all you are going to get. Ie. Distinguishing between your reader having caught up with the writer and the writer having finished.

    If you are reading the files in seperate threads, then you can afford to have the thread hang around waiting for the file to grow some minimum amount before reading the next line, and if it fails to meet that minimum within a preset amount of time, you read and process whatever is left and call it quits.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Is a file currently being modified?
by Roger (Parson) on Aug 17, 2005 at 05:08 UTC
    You could use '/usr/sbin/fuser' if you are working under Unix.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://484275]
Approved by moot
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2023-02-09 12:06 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (45 votes). Check out past polls.