Reply to RE: [PHP] Ensure only one instance of a script is running

Your name:

Reply:


Posted by Robert Cummings on 03/16/05 20:07

On Wed, 2005-03-16 at 11:12, Jay Blanchard wrote:
> [snip]
> I have a script that inserts data from files uploaded to our server. I
> need
> to make sure that only one instance of this script runs at anyone time,
> can
> anyone tell me how I can do this?
> [/snip]
>
> Don't run another instance. ba-dump ching!
>
> Ok, here is a quick and dirty way to do it, have the script check for
> the existence of a file containing a date and timestamp, if one does not
> exist the script creates one. If it does exist, the script exits. Once
> the script is nearing completion have it destroy the file (unlink).

Here's a simple script which uses directories for locking which is
superior to the above example that doesn't cater to race conditions. It
uses directory creation since directory creation is supposedly atomic on
a non NFS filesystem.

<?
function lockCreate( $name )
{
$myPath = ereg_replace( '/[^/]+$', '/', __FILE__ );
$lockPath = $myPath.$name.'.lock';
$status = @mkdir( $lockPath );

return $status;
}

function lockDestroy( $name )
{
$myPath = ereg_replace( '/[^/]+$', '/', __FILE__ );
$lockPath = $myPath.$name.'.lock';

$status = @rmdir( $lockPath );

return $status;
}

if( !lockCreate( $lockName ) )
{
//
// Script already running.
//
exit();
}

//
// Do stuff here then afterwards remove the lock.
//

lockDestroy( $lockName );
?>

HTH and HAND,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация