SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php exec() and background processes

    Hi guys,

    I am designing a multi-threading class for php, and was wondering if any of you had experience with the exec command.

    I am using the exec command to run a process in the background (using the & flag). However, I would also like to get the output from the script somehow. Obviously this is an issue if we want to continue running the page w/o waiting for the output to complete.

    I was wondering if it would be possible to buffer output as it becomes available? This is what the PHP manual has to say:

    Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program
    My question then is, what are my options? Writing to a file doesn't seem like a good idea.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script that spawns this other process...you want it to be able to collect the output? Or, you just want to be able to collect the output later, in a different script?

  3. #3
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Id like it to be stored in the class that spawns the processes. So I can basically call $class->getOutput() at any time and get the output generated so far. I hope that makes sense.

  4. #4
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    use the tempnam() function to generate a unique filename and pipe the output to that file. when you want to check the progress, do a file_get_contents() on that file.
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That really is the easiest way I know(or use a database instead of a file). The other way I know involves stream_select() and a whole lot of other code.

  6. #6
    SitePoint Addict whydna's Avatar
    Join Date
    Jun 2006
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know if the output to the file is "real time" or does it wait until the entire scripts execution is done?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •