在后台执行Laravel/Symfony/Artisan命令 [英] Execute Laravel/Symfony/Artisan Command in Background

查看:153
本文介绍了在后台执行Laravel/Symfony/Artisan命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在后台执行Laravel长时间运行的过程,以使用Twitter Streaming API.实际上,我需要运行的php artisan CLI命令是

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

nohup php artisan startStreaming > /dev/null 2>&1 &

如果我自己在命令行中运行它,它将运行完美.

If I run that myself in the command line it works perfectly.

这个想法是,我可以通过执行长期运行的artisan命令(该站点需要在后台运行,因为Twitter Streaming连接永远不会结束),单击网站上的一个按钮,通过执行长时间运行的artisan命令来启动该流.通过命令行可以正常工作.

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.

但是无法以编程方式调用该命令.我试图从另一个命令通过callSilent()静默调用它,以及尝试使用Symfony \ Component \ Process \ Process运行artisan命令或运行一个运行上述命令的shell脚本,但我无法弄清楚

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.

更新 如果我将打开流连接的命令放入队列,则会导致队列工作者的进程超时

Update If I queue the command which opens the stream connection, it results in a Process Timeout for the queue worker

我实际上需要一种从PHP类/脚本运行上述命令的方法,但其中PHP脚本不等待该命令的完成/输出.

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.

非常感谢

推荐答案

默认情况下,Symfony进程组件将在当前工作目录getcwd()中执行提供的命令.

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

getcwd()返回的值将不是Laravel安装目录(包含工匠的目录),因此该命令很可能会返回artisan: command not found消息.

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 Symfony\Component\Process\Process;

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

这篇关于在后台执行Laravel/Symfony/Artisan命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆