ebe0d60ce7b87e2b77bb6a3c39d8817cd909c280
[phancap.git] / src / phancap / Executor.php
1 <?php
2 /**
3  * Part of phancap
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Executor
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/phancap.htm
13  */
14 namespace phancap;
15
16 /**
17  * Run a shell command
18  *
19  * @category  Tools
20  * @package   Executor
21  * @author    Christian Weiske <cweiske@cweiske.de>
22  * @copyright 2014 Christian Weiske
23  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24  * @version   Release: @package_version@
25  * @link      http://cweiske.de/phancap.htm
26  */
27 class Executor
28 {
29     /**
30      * Run a shell command and check exit code.
31      *
32      * @param string $cmd Full command including parameters and options
33      *
34      * @return void
35      * @throws \Exception When the exit code is not 0
36      */
37     public static function run($cmd)
38     {
39         exec($cmd . ' 2>&1', $arOutput, $exitcode);
40         if ($exitcode != 0) {
41             //FIXME: do something with $arOutput
42             //echo implode("\n", $arOutput) . "\n";
43             throw new \Exception('Error running cutycapt', $exitcode);
44         }
45     }
46
47     /**
48      * Let the command run for some time. Kill it if it did not exit itself.
49      *
50      * We use the GNU coreutils "timeout" utility instead of the pcntl
51      * extension since pcntl is disabled on mod_php.
52      *
53      * @param string $cmd Full command including parameters and options
54      *
55      * @return void
56      * @throws \Exception When the exit code is not 0
57      */
58     public static function runForSomeTime($cmd, $seconds)
59     {
60         return static::run(
61             'timeout --signal=9 ' . $seconds . 's ' . $cmd
62         );
63     }
64 }
65 ?>