[英]Running a Laravel Artisan command as a new process
So in general you can say exec('MyCommand &> /dev/null &')
in php and MyCommand
will be executed as a seperate process, so your primary php execution can continue on it's merry way. 因此,通常您可以在php中说exec('MyCommand &> /dev/null &')
,而MyCommand
将作为一个单独的进程执行,因此您的主要php执行可以很轻松地继续。
Oddly, if you try to do this using Laravel's Artisan
, something goes sideways. 奇怪的是,如果您尝试使用Laravel的Artisan
进行此操作,则会有些偏斜。 For instance, exec('php artisan command &> /dev/null &')
results, surprisingly, in the process still just hanging until the artisan command finishes. 例如,令人惊讶的是, exec('php artisan command &> /dev/null &')
结果是,该过程仍然挂起,直到artisan命令完成。 If you wrap the artisan command in a bash script, it won't execute at all. 如果将artisan命令包装在bash脚本中,则根本不会执行。
Why is this, and how can I execute an artisan command in a new, detached process? 为什么会这样,以及如何在新的独立流程中执行手工命令?
You'll have to create a new process to run it: 您必须创建一个新进程来运行它:
$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.