diff --git a/src/Process.php b/src/Process.php index 84fbc74..7fafb23 100644 --- a/src/Process.php +++ b/src/Process.php @@ -42,8 +42,12 @@ class Process { proc_terminate($this->process, $signal); } + public function status(): array { // get process status + return proc_get_status($this->process); + } + public function isAlive(): bool { // whether sub process still running - return proc_get_status($this->process)['running']; + return $this->status()['running']; } public function quit(): int { @@ -54,25 +58,25 @@ class Process { } -$p = new Process(['sleep', '10'], $capture = true); -echo "PID -> $p->pid\n"; - -echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; -echo "Sleep 5s...\n"; -sleep(5); - -echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; -echo "Send kill signal\n"; -$p->signal(15); -sleep(1); -echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; - -echo '--------------------------------------------' . PHP_EOL; -echo $p->getStdout(); -echo '--------------------------------------------' . PHP_EOL; -echo $p->getStdout(); -echo '--------------------------------------------' . PHP_EOL; -echo $p->getStderr(); -echo '--------------------------------------------' . PHP_EOL; - -echo "Return code -> " . $p->quit() . "\n"; +//$p = new Process(['sleep', '10'], $capture = true); +//echo "PID -> $p->pid\n"; +// +//echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; +//echo "Sleep 5s...\n"; +//sleep(5); +// +//echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; +//echo "Send kill signal\n"; +//$p->signal(15); +//sleep(1); +//echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; +// +//echo '--------------------------------------------' . PHP_EOL; +//echo $p->getStdout(); +//echo '--------------------------------------------' . PHP_EOL; +//echo $p->getStdout(); +//echo '--------------------------------------------' . PHP_EOL; +//echo $p->getStderr(); +//echo '--------------------------------------------' . PHP_EOL; +// +//echo "Return code -> " . $p->quit() . "\n"; diff --git a/src/demo.php b/src/demo.php index 1dc35b3..ffa565e 100644 --- a/src/demo.php +++ b/src/demo.php @@ -1,9 +1,55 @@ pid . PHP_EOL; +// +//while (true) { +// echo "Check vlmcsd..."; +// if ($vlmcsd->isAlive()) { +// echo "Alive\n"; +// } else { +// echo "Death\n"; +// echo "try to restart\n"; +// } +// sleep(1); +//} + +$nginx = array( + 'command' => ['/usr/sbin/nginx'], + 'pidFile' => '/run/nginx/nginx.pid', +); + +function getPid(string $pidFile): int { // get pid by given file + if (!file_exists($pidFile)) { + return -1; // file not exist + } + $file = fopen($pidFile, 'r'); + if (!is_resource($file)) { + return -1; // file open failed + } + $content = fread($file, filesize($pidFile)); // read pid number + fclose($file); + return intval($content); +} + +$p = new Process($nginx['command']); +sleep(1); +while (True) { + if (getPid($nginx['pidFile']) == -1) { + echo 'nginx exit' . PHP_EOL; + $p = new Process($nginx['command']); + } + $p->status(); + sleep(1); +}