X
X
XXXprog2017-07-09 20:39:02
System administration
XXXprog, 2017-07-09 20:39:02

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"));

And this is the handler
public function shutdown_handler(){
      @fclose($this->fp);
    }

We block like this:
$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

3 answer(s)
A
Alexey Skobkin, 2017-07-09
@XXXprog

flock

4
4X_Pro, 2017-07-09
@XXXXPro

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.

N
Night, 2017-07-09
@maxtm

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 question

Ask a Question

731 491 924 answers to any question