Executing an exec() or system() in PHP and do not wait for output

16,845

Solution 1

Depends on the OS you are using.

For linux:

pclose(popen("php somefile.php &","r"));

notice the amperstand at the end (very important).

For windows:

pclose(popen("start php.exe somefile.php","r"));

here the start keyword is important.

Hope this helps.

Solution 2

This doesn't answer your question directly, but you should consider doing your video conversion work in a background process with either a cron job or using a queue such as Beanstalkd.

This way you can stack up your ffmpeg work in the background without blocking your webserver.

I've had a lot of success with both methods (cron / queue) in the past.

Some other posts about background processes:

php execute a background process

Run a ffmpeg process in the background

Using ffmpeg, PHP and beanstalk

Some tools you might find useful:

http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

PEAR System_Daemon

Pheanstalk, a Beanstalkd library for PHP

Solution 3

What I do:

public function post_create()
{
    ob_end_clean();
    header("Connection: close");
    ignore_user_abort(); // optional
    ob_start();
    echo "Tell ajax to gtfo!";

    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush(); // Strange behaviour, will not work
    flush();            // Unless both are called !
    // Do processing here
}
Share:
16,845
adrianTNT
Author by

adrianTNT

I am a web developer working mostly with PHP, HTML, CSS. Some of my sites are: User Album social network MMFILES - buy/sell web components adrianTNT.com - my web components and scripts

Updated on June 04, 2022

Comments

  • adrianTNT
    adrianTNT almost 2 years

    I want to trigger a shell command in eider exec() or system() from PHP script but it is a task that take a while to complete, is there a way to trigger it and continue running the PHP page load without delay?

    Edit: I am on CentOS 6, PHP 5.3

  • adrianTNT
    adrianTNT over 12 years
    Command runs but the delay is there, this is the full command: shell_exec("ffmpeg -i 'videos/sample.mp4' -qmax 1 -vframes 1 -ss 80 videos/images/1.jpg 2> /dev/null");
  • Uku Loskit
    Uku Loskit over 12 years
    try: shell_exec("nohup ffmpeg -i 'videos/sample.mp4' -qmax 1 -vframes 1 -ss 80 videos/images/1.jpg > /dev/null 2> /dev/null")
  • adrianTNT
    adrianTNT over 12 years
    Same delay, command executed fine. Maybe ffmpeg just acts differently?
  • adrianTNT
    adrianTNT over 12 years
    It would probably work but there should be an easier and more reliable way, like a parameter at the end of exec() or system();
  • hummingBird
    hummingBird over 12 years
    I don't get the -1 level. Guy gave a short answer. Maybe disliked, but still a fair one.
  • adrianTNT
    adrianTNT over 12 years
    I tried exec("php video_processor.php &"); and delay is the same, then also if I set a sleep(10) inside video_processor.php then my parent script also waits for 10 seconds.
  • Eduard Luca
    Eduard Luca over 12 years
    Try with pclose(popen("php video_processor.php &","r")); or pclose(popen("/usr/bin/php video_processor.php &","r")); (I assume your PHP is installed in the default location)
  • adrianTNT
    adrianTNT over 12 years
    It worked with this one: pclose(popen("php video_processor.php &","r")); so then I edited mine and it worked: pclose(popen("ffmpeg -i 'videos/sample.mp4' -qmax 1 -vframes 1 -ss 80 videos/images/1.jpg &","r")); Thank you. :party: :)
  • Uku Loskit
    Uku Loskit over 12 years
    Just tested this myself with PHP and ffmpeg and now it works.
  • Matt Korostoff
    Matt Korostoff about 9 years
    Wow, I can't believe how easy and simple this is to do! Brilliant solution! I have researched this at some length before and every other solution recommended complex strategies for forking or threading a PHP process.
  • MSC
    MSC almost 7 years
    Windows specific comment: I used to use: pclose(popen("start /B php.exe somefile.php","r")); as of Windows 10 Creators Update (1703) - the "/B" background switch for "start" command doesnt work. Glad to see that it isnt needed.
  • user1447420
    user1447420 over 6 years
    After trying any other solution finally managed to make it work using your suggestion. Thank you very much!
  • Zombo
    Zombo almost 4 years
    Seems to work with just header('Content-Length:0');flush(); for me