Embed meta data into generated screenshot file
[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      * @param string $name Command name for exception
34      *
35      * @return void
36      * @throws \Exception When the exit code is not 0
37      */
38     public static function run($cmd, $name)
39     {
40         exec($cmd . ' 2>&1', $arOutput, $exitcode);
41         if ($exitcode != 0) {
42             //FIXME: do something with $arOutput
43             //echo implode("\n", $arOutput) . "\n";
44             throw new \Exception('Error running ' . $name, $exitcode);
45         }
46     }
47
48     /**
49      * Let the command run for some time. Kill it if it did not exit itself.
50      *
51      * We use the GNU coreutils "timeout" utility instead of the pcntl
52      * extension since pcntl is disabled on mod_php.
53      *
54      * @param string $cmd     Full command including parameters and options
55      * @param int    $seconds Number of seconds after which the cmd is killed
56      * @param string $name    Command name for exception
57      *
58      * @return void
59      * @throws \Exception When the exit code is not 0
60      */
61     public static function runForSomeTime($cmd, $seconds, $name)
62     {
63         return static::run(
64             'timeout --signal=9 ' . $seconds . 's ' . $cmd,
65             $name
66         );
67     }
68 }
69 ?>