On Mon, October 30, 2006 12:14 pm, Ahmad Al-Twaijiry wrote:
> I create a php script that will run every minute (by cronjob) and
> update some database tables (php 5, database mysql 5, tables type
> innodb)
>
> the problem is that I want this script to run only one at atime (only
> one process from this script can run )
>
> for example, if the cronjob start the script and the script takes more
> than one minute, then after this one minute the cronjob start another
> process from this script, it should exit once it find there is an old
> process of it running)
>
> what I do now is that when the script start it will check for a tmp
> file (/tmp/script.pid) and if it fine it it will exit.
> if the file  (/tmp/script.pid) is not exists, it will create it and
> start doing the database update
> and when the script finish it will remove the file
>
> any better idea ?

On some systems, it's more common to put your lock file in
/var/shared/locks or somesuch.

You also want to be CERTAIN that you do not have a race condition
between 2 scripts creating the lock file at the same time.  With a
full minute between, you are probably pretty safe on that, but...

You may also want to have a function to unlink the file and use
http://php.net/register_shutdown_function to delete it, rather than
just tacking it on at the end of your script.

And, finally, I recommend that you consider doing "something"
automated if the lock file is more than X minutes "old"  -- You might
choose to just ignore it and proceed, on the assmuption that your
previous run crashed or was killed before it could delete the file. 
Even if everything else above seems like overkill, at least this way
you'll know the process IS running often enough to keep up.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to