I’m running a command from a controller like this.-
$finder = new PhpExecutableFinder();
$phpExecutablePath = $finder->find();
$consolePath = $this->getParameter('kernel.project_dir') . '/bin/console';
$command = "app:command";
$process = new Process([$phpExecutablePath, $consolePath, $command, $this->getUser()->getUsername()]);
$process->start();
$process->wait();
It works but only if I wait
for the process to finish, otherwise I understand the controller action finishes and since the process is somehow linked to it, it doesn’t even has time to start.
Now I’m trying executing nohup
like this.-
$backgroundCommand = sprintf('nohup %s %s %s %s > /dev/null 2>&1 &', $phpExecutablePath, $consolePath, $command, $this->getUser()->getUsername());
shell_exec($backgroundCommand);
But doesn’t seem to work either. If I try to render the output in a file.-
$backgroundCommand = sprintf('nohup %s %s %s >> %s 2>&1 &', $phpExecutablePath, $consolePath, $command, $logFile);
shell_exec($backgroundCommand);
I get a
nohup: can't detach from console: Inappropriate ioctl for device
But I assume this is an error related with the log writing more than the command itself.
So my question is, which is the right way to launch a background command from a controller, and to keep it running even when the controller action is over?