Answer the question
In order to leave comments, you need to log in
How to prevent simultaneous execution of a PHP script by cron?
Good afternoon, there was such a question and I just can not find a universal and simple solution. Can anyone help me understand the topic?
There is a php script that will be run by cron every minute. Since the script can be executed either in 5 seconds, or in half an hour, the task is this - if the last launch has not yet completed, then do not launch a new one so as not to interfere with each other and do one job.
The first thing that came to mind was to create a file at startup and delete it at the end of the work. Checking for its presence, the second copy will not start. The minus is obvious, if the script crashes for some reason and the file is not deleted, then the next cron will not start.
I remembered flock. I threw this option for verification:
$lockfile = fopen("my_script.lock", 'w');
if(flock($lockfile, LOCK_EX | LOCK_NB ) !== true)
die("already running");
echo 'Good';
flush();
sleep(10);
Answer the question
In order to leave comments, you need to log in
$lock = fopen('/tmp/lock_file.lock', 'w');
if ( !($lock && flock($lock, LOCK_EX | LOCK_NB)) ) {
exit( 'already running' );
}
If the script already uses a Mysql connection, you can still use its semaphore implementation without "polluting" the file system.
Look at the function: GET_LOCK()
This method doesn't have the drawback you described. When the process that made the lock dies, the lock is released automatically.
P,S, If you are already using files, see register_shutdown_function() , where you can delete the lock file in an emergency.
$fh = fopen(__FILE__, 'r');
if (!flock($fh, LOCK_EX | LOCK_NB)) {
die('Script is already running!');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question