1

I have a PHP script that runs on a shared hosting environment server. This PHP script takes a long time to run. It may take 20 to 30 minutes to finish a run. It is a recurring background process. However I do not have control over when the process starts (it could be triggered every five minutes, or every three hours, no one knows).

Anyway, at the beginnin of this script I would like to detect if the previous process is still running, if the earlier run is still running and has not finished, then I would not run the script again. If it is not running, then I run the new process.

In other words, here is a pseudo code. Let's call the script abc.php

1. Start script abc.php
2. Check if an older version of abc.phh is still running. If it is running, then terminate
3. If it is not running, then continue with abc.php and do your work which might take 30 minutes or more

How can I do that? Please keep in mind this is shared hosting.

UPDATE: I was thinking of using a DB detection mechanism. So, when the script starts, it will set a value in a DB as 'STARTED=TRUE', when done, it will set 'STARTED=FALSE'. However this solution is not proper, because there is no garantee that the script will terminate properly. It might get interrupted, and therefore may not update the STARTED value to FALSE. So the DB solution is out of the question. It has to be a process detection of some sort, or maybe a different solution that I did not think off. Thanks.

8
  • Are you running Linux? Commented Feb 7, 2014 at 18:19
  • You can create a lock file e.g. scriptname.lock to the directory your script runs at the beginning of the script and delete it at the end. After this you can check if file exists at the beginning if it does just die the script Commented Feb 7, 2014 at 18:19
  • Do I understand correctly that this is a CLI process? Commented Feb 7, 2014 at 18:19
  • @engvrdr How is that better than the non-acceptable DB flag approach? Commented Feb 7, 2014 at 18:20
  • @sirago Development is Windows XAMPP. Production server is Linux. Commented Feb 7, 2014 at 18:21

2 Answers 2

1

If this is a CGI process, I would try using exec + ps, if the latter is available in your environment. A quick SO search turns up this answer: https://stackoverflow.com/a/7182595/177920

You'll need to have a script that is responsible for (and separate from) checking to see if your target script is running, of course, otherwise you'll always see that your target script is running based on the order of ops in your "psuedo code".

Sign up to request clarification or add additional context in comments.

Comments

0

You can implement a simple locking mechanism: Create a tmp lock file when script starts and check before if the lock file already exists. If it does, dont run the script, it it doesnt create a lock file and run the script. At then end of successful run, delete the lock file so that it will run properly next time.

if(!locked()) {
    lock();
    // your code here
    unlock();
} else {
    echo "script already running";
}

function lock()   { file_put_contents("write.lock", 'running'); }
function locked() { return file_exists("write.lock"); }
function unlock() { return unlink("write.lock"); }

3 Comments

There isn't a guarantee that the script will terminate correctly. Therefore I may not be able to unlock the file.
You can use try catch around // your code here and unlock in the catch part if it threw any exception. So the next run will be successful.
I am not sure this would work. Because the try will not terminate, rather it will run another instance of the script, which I would like to prevent.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.