写入目标文件时出现require()错误

I use vi to edit a live file on the server. This is a core file required by virtually every page on a moderately busy website. Everything runs fine while I am editing, but when I save my changes about half the time the logs show a user suffered a "Failed opening required 'common.php'" error.

I can only assume the page request came in while the file was being written and vi maintains an exclusive lock on the file during writing, and PHP just gives up immediately instead of queuing for the lock to be released. Can't find any discussion on this issue though. Anybody know? Is there a way to fix this situation? I'm guessing that doing it the "proper" way by editing locally, pushing to the repository then updating the changes to the production site will have the same issue since svn seems to take longer to run then vi takes to write.

The proper fix would involve separate development, testing, and production environments, along with a sane deployment process.

But a simple trick to atomically update a file is to rename the new version over the target. Workflow:

cd temporary-working-dir
cp your/web/stuff/common.php common.php
vi common.php  # make your changes
mv common.php your/web/stuff/common.php

As long as the source and target files are on the same device / partition, mv should be instant, and every request should see either the old or the new version of the file, with no weirdness in between.