Calling PHP script without server timeout

Discussion in 'PHP' started by robster, Feb 16, 2007.

  1. #1
    I need to call a php file from my browser without it timing out(as it normally will when I call it)

    Is there a way I can do this that will prompt the file to run in the background and allow the page I call it from to continue loading without waiting for that script to finish up.... Basically prompting an immediate job call, but something that I dont really need to see the results of for an hour or so.

    Thanks,

    Rob
     
    robster, Feb 16, 2007 IP
  2. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
  3. robster

    robster Peon

    Messages:
    94
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No, that wont work ... The webserver is the problem, not the php...

    So, I want to call a php script(running in the background) when I visit index.php.

    The page visited by me, as the user would be index.php, and it would be to start the processing of the php script which typically lasts 10 minutes or so. During this time, I the user, could be visiting other pages instead of waiting for that script to finish running(or in most cases, the webserver times out)
     
    robster, Feb 16, 2007 IP
  4. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #4
    If I read you post correctly 10 minutes for a php script to run is rediculous. You need to make your page more efficient, it is most likely using a lot of resources as a result.
     
    papa_face, Feb 16, 2007 IP
  5. robster

    robster Peon

    Messages:
    94
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It is parsing different pages, as well as processing those pages. I say 10 minutes, but when it is running for 10 users at once, doing lots of things for each user, then it takes longer.

    But either way, I am looking for a solution to the problem, not advise about script optimization.
     
    robster, Feb 16, 2007 IP
  6. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #6
    Not really because the lack of script optimization is causing this problem.
     
    papa_face, Feb 16, 2007 IP
  7. robster

    robster Peon

    Messages:
    94
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    haha .. ok, these arguments can go on all day...

    Parsing documents from other websites can take a long time, period....

    The point is, I understand that it will take a while to process, I understand the resources required, but I still want to be able to call it from the web, and let it process in the background....

    Now with those things in mind, do you know of a solution. If not its ok, you dont have to comment on the lack of optimization.
     
    robster, Feb 16, 2007 IP
  8. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #8
    Even if I did know of a solution I wouldn't offer it with your foul attitude towards people that are willing to help.
     
    papa_face, Feb 16, 2007 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,839
    Likes Received:
    4,542
    Best Answers:
    123
    Trophy Points:
    665
    #9
    I don't know about fixing the timeout problem...

    However a staged approach might be appropriate.

    Cron 1 calls the setup, initialises the list of pages to retrieve and gives a status of "pending"
    Cron 2 then runs every x minutes and calls any pages with a status of "pending". Evenutally it'll be doing nothing.
     
    sarahk, Feb 16, 2007 IP
  10. robster

    robster Peon

    Messages:
    94
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That could work ...

    I would prefer to have something that starts immediately upon, say, hitting a form button... which would then take me to the next page(step) in the users process... and then by the time they are done with the different steps, the script has finished running and will show the data on the main page when the user returns to it later.

    I would think that there would be some sort of solution for this as it would allow a user to not experience the long processing that I require, but still be able to reap the benefits of it ASAP.

    Thanks for the proposed solution, Sarah... I think that would definately work as a plan B if there is nothing that would complete the task exactly as I desire it.

    Can I call exec() to do anything like this possibly??
     
    robster, Feb 16, 2007 IP
  11. sarahk

    sarahk iTamer Staff

    Messages:
    28,839
    Likes Received:
    4,542
    Best Answers:
    123
    Trophy Points:
    665
    #11
    sarahk, Feb 16, 2007 IP
  12. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #12
    As was said earlier...you can use exec if that function has not been disabled.

    Let's say you have a script called 'longRunning.php' and this is the one that needs to run in the background. If you simply call exec() with the parent script it will hang waiting on it to complete. Thus, this does not solve your issue.

    In order for you to do this successfully you need to do two things: (1) redirect output somewhere other than stout (release the streams) and (2) actually send the process to the background. Here is an example:

    
    exec('/usr/bin/php ./longRunning.php > /dev/null 2>&1 &');
    
    PHP:
    Enjoy!

    Bobby
     
    Chemo, Feb 16, 2007 IP
  13. robster

    robster Peon

    Messages:
    94
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Awesome ...

    I do have exec privileges...

    Can you break down that exec() call and tell me what each part of the call is doing??

    Thanks for the help so far!!
     
    robster, Feb 16, 2007 IP
  14. aco4god

    aco4god Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Yep, you could use shell_exec() or exec() to execute system commands. This will allow you to run the script in the background. You could also have cron configured run the script automatically. Though note that having exec, shell_exec() and certain other functions is very risky so you better make sure your script is bullet proof. If it isn't, you can always enable it with ini_set().
     
    aco4god, Feb 16, 2007 IP