SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Run Process in Background

    I have a linux script my PHP file calls, which takes a very long time to execute. I would much rather PHP initialize the command, and then not wait for output, and just continue processing the PHP file.

    Is there a way to achieve this?

  2. #2
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Google for "PHP forking". There are two main ways to do it, but what you probably want if you are initiating the script from a browser, is the method that uses exec and then redirects the output to dev/null.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That sounds like it would still hang while it waits for the command to finish, it just won't output anything.

  4. #4
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Only if you do not re-direct the output. I was really hoping you would take my advice and just do the search, so I wouldn't have to. You will see from the examples that it does work. I did the search and below is one of the examples.

    http://joseph.randomnetworks.com/arc...e-fork-in-php/

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm I gave it a try and it appears to be working... excellent!

    I'm no pro with Unix command line... say I had two commands I wanted to run, example:

    ffmpeg -i blah.wmv blah.mp4
    and

    ffmpeg -i blah.wmv blah.flv
    I would connect them with &&

    ffmpeg -i blah.wmv blah.mp4 && ffmpeg -i blah.wmv blah.flv
    To get them to both do it, would I do this?:

    ffmpeg -i blah.wmv blah.mp4 >&- <&- >/dev/null & && ffmpeg -i blah.wmv blah.flv >&- <&- >/dev/null &

    EDIT: After some testing I found my script stops processing after about 10 seconds for some reason...

  6. #6
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote:
    ffmpeg -i blah.wmv blah.mp4 >&- <&- >/dev/null & && ffmpeg -i blah.wmv blah.flv >&- <&- >/dev/null &

    try

    ffmpeg -i blah.wmv blah.mp4 &>/dev/null ; ffmpeg -i blah.wmv blah.mp4 &>/dev/null

    or you could make a bash script and put the parameters into it

    // file convert.sh
    #!/bin/bash
    for i in $*
    do
    ffmpeg -i $i $i.mp4 ;
    done


    then call it from php using

    sh ./conver.sh blah.wmv blah1.wmv blah....wmv blah100.wmv 2>/tmp/error &

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try

    ffmpeg -i blah.wmv blah.mp4 &>/dev/null ; ffmpeg -i blah.wmv blah.mp4 &>/dev/null
    This didn't work, it still hung while waiting for the command to finish

    // file convert.sh
    #!/bin/bash
    for i in $*
    do
    ffmpeg -i $i $i.mp4 ;
    done


    then call it from php using

    sh ./conver.sh blah.wmv blah1.wmv blah....wmv blah100.wmv 2>/tmp/error &
    The 2>/tmp/error & doesn't work, but the batch file is a much better way of doing this.... unfortunately if I use the

    >&- <&- >/dev/null &

    method, it runs in the background, but only for a limited time (the script cancels part way through or something). Not sure whats causing it to do it, it runs perfectly fine without it.

  8. #8
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What OS are you using? Do you have shell access?

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm on Red Hat, and yes I have root shell access (I installed ffmpeg onto the server).

    I also run on Apache 2.2

  10. #10
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    365
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try run ffmpeg in shell in background and see if you can see where it stops

    Code:
    ffmpeg -i blah.wmv blah.mp4 &
    you should still see all the output text. Take not of what ffmpeg display when it stops running

    Also most programms have a verbous mode (using the flag -v) which makes the program output more info. It also help to track down errors.


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
  •