Last active
December 16, 2015 13:29
-
-
Save Moinax/d343547defbeaeebb47b to your computer and use it in GitHub Desktop.
Run command in background from Symfony controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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