6

I want to run a Symfony Console Command from a controller. How to do so is explained in the documentation.

Now my controller waits until the Command is finished. But I want the Command to be executed as a background task (like a cronjob). So the controller doesn't need to wait to complete the Command, only start it.

Is this possible with Symfony Console?

1

1 Answer 1

6

I think you have to use Process component and launch command like that:

$process = new Process('php bin/console your:command');
$process->start();
Sign up to request clarification or add additional context in comments.

6 Comments

This is exactly what I need! I also was looking for exec(), but this has way more functions.
This does not work, in symfony 4 is there a new way to do it ??
I took this example from symfony 4 docs, and I used this code in my projects based on Symfony 4. And it worked. Are you sure that you use start instead of run?
Hello I tried use Symfony\Component\Process\Process; $process = new Process('php bin/console app:import:records' . ' > NUL'); $process->run(); return new Response('successfully done', Response::HTTP_OK); it returns response only but the command in between is not executed. Can anybody help?
@nas why using run() and not start()? start() works fine and was suggested.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.