accessing text files under modification

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

What would happen if PHP script tried to read a .txt file from disk but
a cron job is concurrently modifying the same .txt file ?

I'm going to use Linux.


Re: accessing text files under modification

Quoted text here. Click to load it

If you don't use some form of locking, sooner or later you're going
to read crap out of the .txt file.  Generally, all users of the
file have to cooperate with the locking.

A simple form of updating by the cron job:  write the modified
version of the file under a different name in the same directory.
Rename it into place, deleting the original file.  Readers see the
old version or the new one:  never a nonexistent file, never a
half-modified file.  This falls apart, however, when the cron job
runs overtime and a second instance of it starts modifying the file
before the first is finished.  Also, the readers have to close their
file descriptor and re-open it to see any changes.

                            Gordon L. Burditt

Re: accessing text files under modification

Thanks, I think I need to be more specific about what I want to do:

I've configured apache to fetch filename.htm if user requests
filename.htm. But if filename.htm does not exist, apache will call

Now, index.php will generate a page, buffer it using ob_start and then
save it as filename.htm (effectively filename.htm is cached as static
file on server hard disk)

Every 10 minutes a cron job deletes filename.htm

So all users accessing filename.htm would be served with the static
version if it exists, otherwise a latest dynamic version created by

Any comments about using this type of caching technique..?

Site Timeline