All of these file locking schemes, leave open the possibility that another script (or instance of this script) could open the file before the lock is obtained. If that happens someone is going to get a blank file. You might consider using a semphore file for your lock mechanism. Take a look at the excellent
article by Sean Burke in TPJ (Sysadmin Mag) from Feb 2002. He describes a very similar scenario and how he solved it with a semaphore.