Skip to content

Instantly share code, notes, and snippets.

@Moinax
Last active December 16, 2015 13:29
Show Gist options
  • Save Moinax/d343547defbeaeebb47b to your computer and use it in GitHub Desktop.
Save Moinax/d343547defbeaeebb47b to your computer and use it in GitHub Desktop.
Run command in background from Symfony controller
/**
* Call a command in background
*
* @param string $command
* @param array $arguments
*
* @return int
*/
public function runCommand($command, $arguments = array(), $logName = 'tvdb.commands')
{
$root_dir = $this->get('kernel')->getRootDir();
$cmd = sprintf('%s/console %s', $root_dir, $command);
if (count($arguments) > 0) {
$cmd = sprintf($cmd . ' %s', implode(' ', $arguments));
}
$cmd = sprintf("%s --env=%s >> %s 2>&1 & echo $!", $cmd, $this->get('kernel')->getEnvironment(), sprintf('%s/logs/%s.log', $root_dir, $logName));
$process = new Process($cmd);
$process->run();
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
$pid = $process->getOutput();
return $pid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment