From eaac7c5d12eccdab0c4f0037f66aaad712b45451 Mon Sep 17 00:00:00 2001 From: boite Date: Fri, 23 Sep 2016 15:03:34 +0100 Subject: [PATCH] Add timeout option to the shell:exec command. --- src/Command/ShellExecCommand.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Command/ShellExecCommand.php b/src/Command/ShellExecCommand.php index 6220a29..0431922 100644 --- a/src/Command/ShellExecCommand.php +++ b/src/Command/ShellExecCommand.php @@ -4,6 +4,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Console\Input\InputArgument; @@ -22,12 +23,19 @@ public function configure() { $this ->setName('shell:exec') - ->setDescription('Initiate a reboot.') + ->setDescription('Execute the given command line in Bash.') ->addArgument( 'command-line', InputArgument::REQUIRED, 'The command and arguments to be executed by the shell.' ) + ->addOption( + 'timeout', + 't', + InputOption::VALUE_REQUIRED, + 'The maximum permitted execution time in seconds.', + 60 + ) ; } @@ -38,7 +46,8 @@ public function execute(InputInterface $input, OutputInterface $output) 'bash', '-c', $input->getArgument('command-line') - ) + ), + $input->getOption('timeout') ); if ($p->run()) { @@ -58,11 +67,12 @@ public function execute(InputInterface $input, OutputInterface $output) /** * @return \Symfony\Component\Process\Process */ - private function getProcess($arguments) + private function getProcess($arguments, $timeout) { return $this ->processBuilder ->setArguments($arguments) + ->setTimeout($timeout) ->getProcess() ; }