Asynchronous shell exec in PHP

前端 未结 13 2001

I\'ve got a PHP script that needs to invoke a shell script but doesn\'t care at all about the output. The shell script makes a number of SOAP calls and is slow to complete,

相关标签:
13条回答
  • 2020-11-22 01:04

    On linux you can do the following:

    $cmd = 'nohup nice -n 10 php -f php/file.php > log/file.log & printf "%u" $!';
    $pid = shell_exec($cmd);
    

    This will execute the command at the command prompty and then just return the PID, which you can check for > 0 to ensure it worked.

    This question is similar: Does PHP have threading?

    0 讨论(0)
  • 2020-11-22 01:05

    The only way that I found that truly worked for me was:

    shell_exec('./myscript.php | at now & disown')
    
    0 讨论(0)
  • 2020-11-22 01:08

    I also found Symfony Process Component useful for this.

    use Symfony\Component\Process\Process;
    
    $process = new Process('ls -lsa');
    // ... run process in background
    $process->start();
    
    // ... do other things
    
    // ... if you need to wait
    $process->wait();
    
    // ... do things after the process has finished
    

    See how it works in its GitHub repo.

    0 讨论(0)
  • 2020-11-22 01:09

    php-execute-a-background-process has some good suggestions. I think mine is pretty good, but I'm biased :)

    0 讨论(0)
  • 2020-11-22 01:10

    I used at for this, as it is really starting an independent process.

    <?php
        `echo "the command"|at now`;
    ?>
    
    0 讨论(0)
  • 2020-11-22 01:13

    without use queue, you can use the proc_open() like this:

        $descriptorspec = array(
            0 => array("pipe", "r"),
            1 => array("pipe", "w"),
            2 => array("pipe", "w")    //here curaengine log all the info into stderror
        );
        $command = 'ping stackoverflow.com';
        $process = proc_open($command, $descriptorspec, $pipes);
    
    0 讨论(0)
提交回复
热议问题