Jump to content

Running a PHP script for a long time


Rusty3

Recommended Posts

I'm just going to throw out a couple of questions.

 

Is it going to be looping through some sort of array?

What kind of functions are you going to be running?

 

I'm just going to throw out a suggestion as this is something I wonder about myself:

- perhaps using AJAX to repetitively call the php page

- the php page would possibly use sessions to keep track of its progress

- the AJAX function would use the results to determine the error or to continue

Link to comment
Share on other sites

Thanks for the reply. I don't see how ajax could make a difference?! :confused: But,maybe, that's me.

 

Yes, db, array, the usual stuff. Flushing it to keep sending output.

 

I also wonder how to detect when to reload it. Sometimes php scripts stop responding and running but still show up as alive. Till they are killed... but then I would not be keeping it continuosly running....

Link to comment
Share on other sites

Well, the detecting when to reload it would be my idea of using AJAX. AFAIK, there's no way to determine how a PHP page is running (if at all) without checking it after each loop... unless you just check the database process calls instead (<-- this might be viable, if I knew how to check the processes running or how to determine whether that process call is from that specific script and not another script).

 

If you had a cron job that would execute the PHP page and if it resulted in an error, write the error to a file. A separate cron job process would analyze this file every minute or so and if there is an error, reset the cron job?

 

Link to comment
Share on other sites

Before anybody mentions it, I'll explain why this won't work:

 

<meta http-equiv="refresh" content="60">

 

PHP runs server-side, so seeing as how the refresh happens serverside.. it would time-out before the refresh could ever take place.

 

What about something like this:

 

$timeOut = $_SERVER['REQUEST_TIME'] + 59;

while ($whatever) {
    if (time() == $timeOut) {
    die('<meta http-equiv="refresh" content="600">');
    } else {
    //Do code
}

Link to comment
Share on other sites

The script triggering its own refresh! Very clever. Will try it out...

 

Wait a minute, it won't cut the cheese! :(

 

Instead of $timeOut, I tought about putting the refresh test on the main loop, but its eating its own tale. If script is dead, loop will not go on, refresh will not be triggered ...

Link to comment
Share on other sites

If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends.

Link to comment
Share on other sites

If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends.

 

How do you run the AJAX code? Via a cron job or what?

Link to comment
Share on other sites

If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends.

 

How do you run the AJAX code? Via a cron job or what?

 

Good question.

Link to comment
Share on other sites

Example:

session_start();
$timer = 5;
$refresh = $_SERVER['REQUEST_TIME'] + $timer;

if (!isset($_SESSION['i'])) {
$_SESSION['i'] = 0;
}

while (1) {
if (time() >= $refresh) {
	die('<meta http-equiv="refresh" content="0">' . $_SESSION['i']);
} else {
	$_SESSION['i']++;
}
}

 

This script counts as high as it can, and refreshes every five seconds. After the refresh, it continues counting from where it left off. Any issues with my logic here? Obviously a five second refresh is unnecessary, but it's easier than waiting around for a full minute while testing the script. ;)

Link to comment
Share on other sites

kratsg: yes, I think there's a way of cron not running a script that its already running and if a cron is called every second, it would work. But its not an elegant solution...

Cron tasks frequency can only be set to per minute.

 

cron task should check for a lock file.
Terminate if lock file exists
If lock file not exist, create lock file
... run tasks
clear lock file
terminate

Link to comment
Share on other sites

 

cron task should check for a lock file.
Terminate if lock file exists
If lock file not exist, create lock file
... run tasks
clear lock file
terminate

 

Yes, that's the solution I was thinking about. A lock file or a lock key on the db. Cron would call a control php to do that, with a timeout of 59sec.

Link to comment
Share on other sites

If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends.

 

I run it by initiating the process via button on the web page. The process I built above was meant to be initiated by the user and not a nightly process. But, as I first stated, this would be a whole lot easier to help with if the end goal was better explained.

 

How do you run the AJAX code? Via a cron job or what?

 

Good question.

Link to comment
Share on other sites

If you would give a better explanation of what you are trying to achieve a suitable answer might be given. The AJAX idea could definitely be a viable one. I have some code to process thousands of folders with files int hem. Rather than do them all in one shot I have the script first "grab" all the folders and paths and store them in the db, then each successive AJAX call will process the next folder. Each time a folder completes the AJAX function on the client page is triggered and will send a another request that processes the next folder. This all continues indefinitely until the last folder is processed and an appropriate code is returned back to the page making the AJAX call and it then ends.

 

I run it by initiating the process via button on the web page. The process I built above was meant to be initiated by the user and not a nightly process. But, as I first stated, this would be a whole lot easier to help with if the end goal was better explained.

 

How do you run the AJAX code? Via a cron job or what?

 

Good question.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.