Search code examples
phplong-running-processes

long processes php


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


Solution

  • 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.