我有一个PHP脚本,需要调用shell脚本,但根本不关心输出。shell脚本执行了许多SOAP调用,完成起来很慢,因此我不想在PHP请求等待应答时降低它的速度。事实上,PHP请求应该能够在不终止shell进程的情况下退出。

我已经研究了各种exec()、shell_exec()、pcntl_fork()等函数,但它们似乎都不能提供我想要的东西。(或者,即使他们这样做,我也不清楚是如何做到的。)有什么建议吗?


当前回答

在linux上,您可以执行以下操作:

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

这将在命令提示符处执行命令,然后只返回PID,您可以检查> 0以确保它工作正常。

这个问题类似:PHP有线程吗?

其他回答

做这件事的正确方法是

叉() 塞西德() execve()

Fork Fork, setsid告诉当前进程成为主进程(没有父进程),execve告诉调用进程被被调用进程取代。这样父母可以在不影响孩子的情况下退出。

 $pid=pcntl_fork();
 if($pid==0)
 {
   posix_setsid();
   pcntl_exec($cmd,$args,$_ENV);
   // child becomes the standalone detached process
 }

 // parent's stuff
 exit();

在linux上,您可以执行以下操作:

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

这将在命令提示符处执行命令,然后只返回PID,您可以检查> 0以确保它工作正常。

这个问题类似:PHP有线程吗?

我发现唯一对我有效的方法是:

shell_exec('./myscript.php | at now & disown')

我用这个…

/** 
 * Asynchronously execute/include a PHP file. Does not record the output of the file anywhere.  
 * Relies on the PHP_PATH config constant.
 *
 * @param string $filename  file to execute
 * @param string $options   (optional) arguments to pass to file via the command line
 */ 
function asyncInclude($filename, $options = '') {
    exec(PHP_PATH . " -f {$filename} {$options} >> /dev/null &");
}

(PHP_PATH是一个const类型,如define('PHP_PATH', '/opt/bin/php5')或类似定义)

它通过命令行传入参数。要在PHP中读取它们,请参阅argv。

如果没有使用队列,你可以像这样使用proc_open():

    $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);