0

I am trying to run console command from my controller using Process Component, but it doesn't work.

This is my code:

     $process = new Process('php bin/console mycommand:run');
     $process->setInput($myArg);
     $process->start();

I also tried:

    $process = new Process('php bin/console mycommand:run ' . $myArg)
    $process->start();

I run my command using:

php bin/console mycommand:run my_argument

Could you tell me what I am doing wrong?

SakuragiRokurota
  • 185
  • 1
  • 3
  • 12

1 Answers1

1

I think the problem is path. Anyway you should consider not using Process to call Symfony Commands. Console components is allowing to call commands e.g. in controller.

Example from docs:

// src/Controller/SpoolController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;

class SpoolController extends Controller
{
    public function sendSpoolAction($messages = 10, KernelInterface $kernel)
    {
        $application = new Application($kernel);
        $application->setAutoExit(false);

        $input = new ArrayInput(array(
           'command' => 'swiftmailer:spool:send',
           // (optional) define the value of command arguments
           'fooArgument' => 'barValue',
           // (optional) pass options to the command
           '--message-limit' => $messages,
        ));

        // You can use NullOutput() if you don't need the output
        $output = new BufferedOutput();
        $application->run($input, $output);

        // return the output, don't use if you used NullOutput()
        $content = $output->fetch();

        // return new Response(""), if you used NullOutput()
        return new Response($content);
    }
}

Using this way you're sure that code will always work. When PHP is in safe mode (exec etc. are turned off) Process component is useless. Additionally you don't need to care about paths and other things otherwise the situation you're call command 'manually'.

You can read more about calling commands from controller here.

Wolen
  • 874
  • 6
  • 15
  • I tried this method before and it worked out, but I also wanted to be able to stop the command after it run. With Process I could just retrive the pid somewhere and later kill the process. What are my options with this solution if I would be able to kill the command, I heard it's possible with pcntl_signals, but as you can see in my previous thread - https://stackoverflow.com/questions/47941237/symfony3-command-pcntl-doesnt-works It didn't worked for me. – SakuragiRokurota Dec 27 '17 at 13:22
  • @SakuragiRokurota your best bet is using message queue. This way your controller only sends message to the queue, and command processing will be separated from HTTP request. – svgrafov Dec 27 '17 at 14:46