How to run background process in PHP

Discussion in 'PHP' started by sithik_frns, May 19, 2010.

  1. #1
    Hi,

    I have a PHP application in which I get data from users and validate them. On successful validation I should initiate a process which may take time to complete. So I need to execute the process in the background.

    I tried exec and all. Unfortunately I can't bring what I need.

    Can someone here help me in this regard?

    Thanks in advance!
     
    sithik_frns, May 19, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    What is the ultimate goal of this? What is meant to happen once the background script has finished processing?

    Depending on what you re trying to acheive you have several options....

    * Do it as a non blocking fork - might get unmanageable if you have to process many entires at once due to memory constraints
    * Add the request to a database or such, and have a cron script check for unprocessed entires every 5 minutes or so (this creates more of a delay)
    * Send the request through to an asynch web service to process
     
    lukeg32, May 19, 2010 IP
  3. sithik_frns

    sithik_frns Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply buddy.

    I actually want to initiate a PHP script (Say 'Job A') in background on completion of a validation script (Say 'Validation'). Once Job A is initiated, Validation script will tell the customer as 'Your request has been submitted' and this should not wait for Job A to complete its job.

    Since there should not be any delay I dont want to use Crons. . Also calling an async web service also make delays I guess.

    I'm not aware of forking, but I hope this would be my best option to work with. Because I dont wan to manage the initiated script, but the new script should be closed properly once it done its job, coz, its going to be a heavy system.

    So can you help me to write fork processes?
     
    sithik_frns, May 19, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    ok cool.

    Actually no - 'validation' would pass a request to a listening web service which would take the request, respond with a suitable message, (E.G true on ok, false on error) and it would then process the request. validation is then free to finish
    up what it was doing.

    You can do this with a simple XMLRPC server POSTing the data to it, for example.....

    I would guess that it, on completion, should update an entry into a database, notify customer or other such actions but it would be independant on validation either way.


    Forking hell.... :)

    (non-blocking) forking within the same script is a tad more complex. Have you ever worked with sockets in PHP (or C/perl) before? By going down this route you have to be certain to keep tabs on *everything* or else you will run into memory leaks/problems. To be honest, if you are considering goign this way, it is probsbly much better to write your own RPC non blocking server and pass the request to it (as above)

    You can find out more about sockets here; there is also sample server code in the comments which should give you a heads
    up.....

    http://php.net/manual/en/function.socket-accept.php

    Its not too complex to setup your own server to listen on a certain port; security wise you can block all access from the outside world too. Give me a shout if you need anything else.
     
    lukeg32, May 19, 2010 IP
  5. sithik_frns

    sithik_frns Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Im so confused now. I need the better solution now.

    I don't think XMLRPC is required here.

    Some ppl told me that exec will do initiate a process and the function will not wait for the response from the process.

    What you think about this?

    I appreciate for your support! Thanks a ton!
     
    sithik_frns, May 19, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    You can use exec to initiate a command line program; in this case, you can call an external php script to process the data.

    My thoughts would still be to a proper interface because exec, like eval, is poor programming practice......

    But hey - its your project..... :) if you still want to use it, and If you want to do it in a non blocking fashion you can do this;

    exec('/usr/bin/php /path/to/your/otherscript.php > /dev/null 2>&1 &');
    PHP:
     
    lukeg32, May 19, 2010 IP
  7. sithik_frns

    sithik_frns Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hey,

    I really require the best option. I don't know mine is a good option or not. Let me try both XMLRPC and exec. Then I will check which gives more performance on heavy load.

    I will get back to you once I tested these two things..

    Anyway, thanks lukeg32 for your support :)
     
    sithik_frns, May 19, 2010 IP
  8. abstractworld

    abstractworld Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    wat about AJAX???
    you can use it...
     
    abstractworld, May 19, 2010 IP
  9. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #9
    Executing your script in the background as suggested here is going to be the least painful and most efficient way of doing this. I would make sure to put a ini_set("max_execution_time".. at the top of the unattended script. This way you don't need to mess with cron, and the script will destruct after whatever time you specify. You can pass parameters to the unattended script as well if you need to.

    Also, eval is sloppy and is one of the biggest potential security holes you can possibly put into a script. I would avoid it at all costs.
     
    jestep, May 20, 2010 IP