繁体   English   中英

将Laravel Artisan命令作为新过程运行

[英]Running a Laravel Artisan command as a new process

因此,通常您可以在php中说exec('MyCommand &> /dev/null &') ,而MyCommand将作为一个单独的进程执行,因此您的主要php执行可以很轻松地继续。

奇怪的是,如果您尝试使用Laravel的Artisan进行此操作,则会有些偏斜。 例如,令人惊讶的是, exec('php artisan command &> /dev/null &')结果是,该过程仍然挂起,直到artisan命令完成。 如果将artisan命令包装在bash脚本中,则根本不会执行。

为什么会这样,以及如何在新的独立流程中执行手工命令?

您必须创建一个新进程来运行它:

$pid = pcntl_fork();

switch($pid)
{
    case -1:    // pcntl_fork() failed
        die('could not fork');

    case 0:    // you're in the new (child) process
        exec("php artisan command");

        // controll goes further down ONLY if exec() fails
        echo 'exec() failed';

    default:  // you're in the main (parent) process in which the script is running
        echo "hello";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM