如何在后台运行自定义 Symfony2 命令 [英] How to run custom Symfony2 command in background

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

问题描述

Symfony2 使开发人员能够创建自己的命令行命令.它们可以从命令行执行,也可以从控制器执行.根据官方 Symfony2 文档,可以这样做:

Symfony2 enables developers to create their own command-line commands. They can be executed from command line, but also from the controller. According to official Symfony2 documentation, it can be done like that:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $command = $this->getApplication()->find('demo:greet');

    $arguments = array(
        ...
    );

    $input = new ArrayInput($arguments);
    $returnCode = $command->run($input, $output);

}

但在这种情况下,我们等待命令完成执行并返回返回码.

But in this situation we wait for the command to finish it's execution and return the return code.

我怎样才能从控制器,在不等待它完成执行的情况下执行将其分叉到后台的命令?

How can I, from controller, execute command forking it to background without waiting for it to finish execution?

换句话说,相当于什么

$ nohup php app/console demo:greet &

推荐答案

根据文档我认为没有这样的选项:http://api.symfony.com/2.1/Symfony/Component/Console/Application.html

According to the documentation I don't think there is such an option: http://api.symfony.com/2.1/Symfony/Component/Console/Application.html

但是关于您要实现的目标,我认为您应该改用流程组件:

But regarding what you are trying to achieve, I think you should use the process component instead:

use Symfony\Component\Process\Process;

$process = new Process('ls -lsa');
$process->run(function ($type, $buffer) {
    if ('err' === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    }
});

并且如文档中所述如果您希望能够实时获得一些反馈,只需将匿名函数传递给 run() 方法即可".

And as mentioned in the documentation "if you want to be able to get some feedback in real-time, just pass an anonymous function to the run() method".

http://symfony.com/doc/master/components/process.html

这篇关于如何在后台运行自定义 Symfony2 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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