Execute Laravel/Symfony/Artisan Command in Background

随声附和 提交于 2019-12-05 10:21:46

The Symfony Process Component by default will execute the supplied command within the current working directory, getcwd().

The value returned by getcwd() will not be the Laravel install directory (the directory that contains artisan), so the command will most likely returning a artisan: command not found message.

It isn't mentioned in the Process Component documentation but if you take a look at the class file, we can see that the construct allows us to provide a directory as the second parameter.

public function __construct(
    $commandline, 
    $cwd = null, 
    array $env = null, 
    $input = null, 
    $timeout = 60, array 
    $options = array())

You could execute your desired command asynchronously by supplying the second parameter when you initialise the class:

use Symphony\Component\Process\Process;

$process = new Process('php artisan startStreaming > /dev/null 2>&1 &', 'path/to/artisan'); 
$process->start();

How about queuing the execution of the command via Laravels build-in queues?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!