Ruben Amezcua
Ruben Amezcua

Reputation: 47

How to know if php file is running?

I have a php file which I think the code doesn't matter.

The problem is that I cannot allow it to run more than 3 times at a time since it makes many requests to the mysql database and becomes saturated.

I've seen some traffic light in php but I don't like it.

Do you have any better ideas?

Thank you.

Upvotes: 1

Views: 112

Answers (1)

user2342558
user2342558

Reputation: 6737

These are some approach you can follow to achieve your goal:

  • the script create a file like "scriptNameIsRunning.txt"; the script first check for this file (do that at the top of the script): if it's found the script immediately ends. When the script complete delete that file (do that at the end of the script).
  • the same as the above but storing the log in the database instead of using a file

To handle the case in which the script may fail during its execution, you may insert a try...catch block and delete the file in the finally clause:

try
{
    // create the file
    // script work
}
catch(Exception $e)
{
    // handle the exception, e.g.:
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
finally
{
    // delete the file
}

Upvotes: 1

Related Questions