I have a server script that is started by cron every 10 minutes and does some operations with database, so it can be running for several minutes. So as you see, the probability of overlapping the update process and script work is quite high while I can't afford that, since it can lead to DB corruption, etc.
My first thougt was to make some kind of locking mechanism, but everything I imagine becomes quite sophysticated: e.g. I'm thiking of having a PID file for running script with some kind of lock, but that leads to so many devil details, like correct locking, avoiding situations when script exits by error leaving .pid file on disk (preventing following runs), etc., etc.
My question then: is there a simple way to do this or some library/tool that solves this kind of problem?
Thank you!