Answer the question
In order to leave comments, you need to log in
How to block script execution if it is already running?
Tell me how to block the re-run of the script by cron, if the previous run of the script has not completed its work yet, or completed but with an error (fatal error)?
We are talking about PHP and Cron
. In general, the solution so far is this:
This is in the constructor
register_shutdown_function(array(&$this,"shutdown_handler"));
public function shutdown_handler(){
@fclose($this->fp);
}
$this->fp = fopen('tmp/lock.txt', 'w');
if(!flock($this->fp, LOCK_EX | LOCK_NB)) {
echo("Script already running");
exit(-1);
}
Answer the question
In order to leave comments, you need to log in
Do this: check for the presence of a file with a certain name somewhere in / tmp or / var / run, if it exists, then immediately exit the script, if not, then create such a file, then perform the necessary work and delete the file before completion of the script, if it passes correctly.
At startup, write your PID to a file.
At startup, it reads the PID, check - if the process is alive and it is yours - exit.
If there is no process, start.
They hope that your script will be finalized to the end and it’s not worth deleting the file, anything can happen.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question