Skip to content

Commit 41bc3e2

Browse files
author
Nate Good
committed
Merge pull request #150 from bpedro/feature/kill-web-server-fix
Fix web server kill when the SIGKILL constant isn't available
2 parents 7beda47 + 7342948 commit 41bc3e2

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

tests/bootstrap-server.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,40 @@
33
$php_version = phpversion();
44
$php_major = floatval(substr($php_version, 0, 3));
55

6+
// Define SIGKILL if pcntl is not found
7+
if (!function_exists('pcntl_signal')) {
8+
define('SIGKILL', 9);
9+
}
10+
611
if ($php_major < 5.4) {
7-
define('WITHOUT_SERVER', true);
12+
define('WITHOUT_SERVER', true);
813
} else {
9-
// Command that starts the built-in web server
10-
$command = sprintf('php -S %s:%d -t %s >./server.log 2>&1 & echo $!', WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);
11-
12-
// Execute the command and store the process ID
13-
$output = array();
14-
exec($command, $output, $exit_code);
15-
16-
// sleep for a second to let server come up
17-
sleep(1);
18-
$pid = (int) $output[0];
19-
20-
// check server.log to see if it failed to start
21-
$server_logs = file_get_contents("./server.log");
22-
if (strpos($server_logs, "Fail") !== false) {
23-
// server failed to start for some reason
24-
print "Failed to start server! Logs:" . PHP_EOL . PHP_EOL;
25-
print_r($server_logs);
26-
exit(1);
27-
}
28-
29-
echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL;
30-
31-
register_shutdown_function(function() {
32-
// cleanup after ourselves -- remove log file, shut down server
33-
global $pid;
34-
unlink("./server.log");
35-
posix_kill($pid, SIGKILL);
36-
});
14+
// Command that starts the built-in web server
15+
$command = sprintf('php -S %s:%d -t %s >./server.log 2>&1 & echo $!', WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);
16+
17+
// Execute the command and store the process ID
18+
$output = array();
19+
exec($command, $output, $exit_code);
20+
21+
// sleep for a second to let server come up
22+
sleep(1);
23+
$pid = (int) $output[0];
24+
25+
// check server.log to see if it failed to start
26+
$server_logs = file_get_contents("./server.log");
27+
if (strpos($server_logs, "Fail") !== false) {
28+
// server failed to start for some reason
29+
print "Failed to start server! Logs:" . PHP_EOL . PHP_EOL;
30+
print_r($server_logs);
31+
exit(1);
32+
}
33+
34+
echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL;
35+
36+
register_shutdown_function(function() {
37+
// cleanup after ourselves -- remove log file, shut down server
38+
global $pid;
39+
unlink("./server.log");
40+
posix_kill($pid, SIGKILL);
41+
});
3742
}

0 commit comments

Comments
 (0)