簡體   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