From a100e0b6fd26df0183bd22dbdb10c7a36d6d9ce5 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Tue, 26 Apr 2016 18:22:27 +0200 Subject: [PATCH] Change how the program listening are killed Following recommendations from http://unix.stackexchange.com/a/8927, I introduced a softer way of terminating the programs listening on the wanted port. --- src/Server.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Server.php b/src/Server.php index 737658f..f662814 100644 --- a/src/Server.php +++ b/src/Server.php @@ -115,15 +115,19 @@ public function processMessage($message, $client) private function killProgramsOnDreddPort() { - // get any programs running on the dredd port - $string = shell_exec("lsof -i tcp:61321"); + foreach ([SIGTERM, SIGINT, SIGHUP, SIGKILL] as $signal) { + // get any programs running on the dredd port + if ($string = shell_exec("lsof -i tcp:61321")) { + $regex = '/(?:php\s*)(\d*)/'; - $regex = '/(?:php\s*)(\d*)/'; + // get matches if any programs are returned + preg_match($regex, $string, $matches); - // get matches if any programs are returned - preg_match($regex, $string, $matches); + // execute kill command so server can listen on port + shell_exec("kill -$signal {$matches[1]}"); - // execute kill command so server can listen on port - shell_exec("kill -9 {$matches[1]}"); + sleep(3); + } + } } }