FortuneSoldier
FortuneSoldier

Reputation: 508

How could I run a command in like an background task in Symfony application?

I have to set up a job manager for the console command that I have created. The console command needs from 20 - 30 minutes to finish. This console command is writing into the database multiple queries and let's assume that it can't be shrink-ed to a less time.

I have all created in Symfony2.8, but I am a really newbie using it. So I enter link description here

All I need is to run this command like a background task. Without touching the frontend of the application. Could anyone help me out ?

Ah, all right. This needs to be done regularly, but on a user/clients request.

Upvotes: 1

Views: 1499

Answers (2)

Yarimadam
Yarimadam

Reputation: 1183

You need The Process Component.

You can achieve php running in background the following way:

$builder = new ProcessBuilder();
$builder->setPrefix('/usr/bin/php'); // put your php executable here
$cmd = $builder->setArguments(['/full/path/to/php/file/you/want/to/run.php', 'argumentOne', 'argumentTwo'])
      ->getProcess()
      ->getCommandLine();

$process = new Process($cmd . '  > /dev/null 2>&1 &'); // async trick here
$process->run();

Upvotes: 1

Alister Bulman
Alister Bulman

Reputation: 35149

It very much depends on what it needs to do.

Just run once it once-only? Login onto the server, use tmux or screen to keep something running, even if you disconnect, and then run the command (bin/console name-of-command).

Running regularly - put an entry into crontab for the user that needs to run it.

Without knowing what the script needs to do, and how often it would be done (if any), it's hard to say what the nexts steps could be.

Upvotes: 2

Related Questions