Hi im trying to make a small script that executes an external program , mpg321,
I have tried to use shell_exec, exec and passthru like this :---

<?php shell_exec('mpg321 mymp3.mp3'); ?>

mpg321 is started fine, only the page continues to load in my browser window until playback has finished..

is there any way to execute the command and move on with out waiting for it to complete ?

Thanks in advance 🙂

    Have the program direct output elsewhere, otherwise PHP waits until it has the output so it can display it. You might want to just use exec rather than shell_exec. Here's an example (albeit on Windows)

    <?php
    exec('ping www.google.co.uk > C:/ping.txt'); // Save output to a file
    while(!file_exists("C:/ping.txt")){
     sleep(2);
    }
    echo file_get_contents("C:/ping.txt");
    ?>
    

      Not sure if that'll work actually Shrike because it's still a foreground process. You won't get anything back from stdout but php will still wait for it to finnish processing (incidentally although you're redirecting stdout, you're not redirecting stderr so there could still be output). To make a process run in the background you append an apersand (&) to the command.

      shell_exec('mpg321 mymp3.mp3 &');
      

      HTH
      Bubble

        The method below will also work, however it won't interrupt the process if the server goes hay-wire.

        shell_exec('nohup mpg321 mymp3.mp3  >/dev/null 2>&1 &');
        

          Thanks Guys That Worked Great!!

            Ahh Windows ping wasn't the best program to test this with then 😉

              Write a Reply...