views:

89

answers:

5
+2  Q: 

long processes php

hi,

i need to run a really long php script (four and half, five hours).

the script sometimes runs successfully, but sometimes gets killed inexplicably (poss something to do with the shared hosting??).

i think that the solution maybe to run the script is smaller chunks.

in order to do this i have written a script that stores it's status & position in an xml file, and executes one chunk of the script, before moving the position on.

i am having problems hooking up the last bit of the script, which should end the current process & re-execute the script.

or maybe i am barking up the wrong tree completely!

i have read through what i can find on SO and elsewhere but i'm still none the wiser :(

please help!!!

dan

+1  A: 

PHP isn't the language of choice for running long scripts. I'm assuming you're running this in a browser? Try logging in via SSH and running the script with php /path/to/script.php.

If this is something you can run locally you could install a basic Apache / PHP install on your local machine and then run it. Use this if you don't have shell access.

Josh K
what language would be better?i can run it locally, but i'd rather run it on remote hosting.
significance
C, C++, Java. Not a scripting language. Nothing wrong with PHP, and I've abused it myself for various file processing things. But if you're munching on it for 4 hrs you need to be looking into an alternative. Not only will it be more stable for the duration of the run but it will most likely be faster then the original PHP program.
Josh K
cool - i am self taught php web app design extending it to data processing... it seems to do pretty well, even when processing 36k records through a pretty complex script.i will take a look at those langauges. would ruby be appropriate?i'm not eager to give up the conveniences of php though, i must say...
significance
Ruby is also a scripting language. If you want to find something that's similar to PHP in synatx, take a look at Java.
Josh K
+1  A: 

If possible, I would try to split the long running process to be its own process. Make your PHP script launch the calculations and then look for clues that it might be finished (something like a state file that contains the current calculation progress). You could then even use AJAX techniques to give the user a nice little progressbar ;)

kigurai
+1  A: 

If you have access to cron at this server you may run your script every minute to process next chunk of data.

Kamil Szot
i've actually ended up using a combination of your, kiguri and kalelias' solution - i've made my script so that it picks up where it left off when re-invoked, and plan to have a cron job to check it's still running every 10 minutes or so.
significance
A: 

I would agree with Josh and suggest that you program this some other way like a desktop application if possible. PHP was sure not built for something like this which requires the script to run for 4-5 hours. This can be sure to max_execution_time or script eating up the CPU in shared hosting but moving to desktop app or SSH is the workout that I will suggest.

Naveen Bhalla
+2  A: 

Considering you have a script that run's forever but won't cause inconsistend data, you could use a cronjob.

The problem is that you need to know if your script is still running, because you likely don't wanna start it twice. Two solutions i have on mind are the process id of the script (getmypid()) or using a timestamp.

For PID:

  • Save PID on script startup (to /tmp/script_pid)
  • Trigger cronjob each minute. Lookup active process with save PID, and start if not found.

You need access to php's exec() (and friends) and command line tools like linux' "ps".

For timestamp:

  • Save timestamp each iteration.
  • Trigger cronjob each minute. If timestamp is older than X, start new process.

You have to figure out how long X should be yourself.

elias