在后台执行 Laravel/Symfony/Artisan 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32216857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute Laravel/Symfony/Artisan Command in Background
提问by ExoticChimp
I need to execute a Laravel long running process in the background for consuming the Twitter Streaming API. Effectively the php artisan CLI command I need to run is
我需要在后台执行 Laravel 长时间运行的进程以使用 Twitter Streaming API。实际上,我需要运行的 php artisan CLI 命令是
nohup php artisan startStreaming > /dev/null 2>&1 &
If I run that myself in the command line it works perfectly.
如果我自己在命令行中运行它,它可以完美运行。
The idea is that I can click a button on the website which kicks off the stream by executing the long running artisan command which starts streaming (needs to run in the background because the Twitter Streaming connection is never ending). Going via the command line works fine.
这个想法是我可以点击网站上的一个按钮,通过执行长时间运行的 artisan 命令来启动流(需要在后台运行,因为 Twitter 流连接永无止境)。通过命令行工作正常。
Calling the command programatically however doesn't work. I've tried calling it silently via callSilent() from another command as well as trying to use Symfony\Component\Process\Process to run the artisan command or run a shell script which runs the above command but I can't figure it out.
然而,以编程方式调用命令不起作用。我尝试通过 callSilent() 从另一个命令静默调用它,并尝试使用 Symfony\Component\Process\Process 来运行 artisan 命令或运行运行上述命令的 shell 脚本,但我无法弄清楚.
UpdateIf I queue the command which opens the stream connection, it results in a Process Timeout for the queue worker
更新如果我将打开流连接的命令排队,则会导致队列工作器的进程超时
I effectively need a way to run the above command from a PHP class/script but where the PHP script does not wait for the completion/output of that command.
我实际上需要一种从 PHP 类/脚本运行上述命令的方法,但 PHP 脚本不等待该命令的完成/输出。
Help much appreciated
非常感谢帮助
回答by Jeemusu
The Symfony Process Component by default will execute the supplied command within the current working directory, getcwd()
.
默认情况下,Symfony 进程组件将在当前工作目录中执行提供的命令,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.
返回的值getcwd()
将不是 Laravel 安装目录(包含 artisan 的目录),因此该命令很可能会返回一条artisan: command not found
消息。
It isn't mentioned in the Process Component documentationbut 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.
Process Component 文档中没有提到它,但是如果您查看类文件,我们可以看到该构造允许我们提供一个目录作为第二个参数。
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 Symfony\Component\Process\Process;
$process = new Process('php artisan startStreaming > /dev/null 2>&1 &', 'path/to/artisan');
$process->start();
回答by Jakob K
How about queuing the execution of the command via Laravels build-in queues?
如何通过 Laravel 内置队列对命令的执行进行排队?