From 163a706bcc118fe9f24a3c299ed4c76774d45395 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 10 Aug 2022 15:39:36 +0800 Subject: [PATCH] update: process daemon --- src/Daemon.php | 51 +++++++++++++++++++++++++++++++++++++ src/demo.php | 69 ++++++++++++++++++-------------------------------- 2 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 src/Daemon.php diff --git a/src/Daemon.php b/src/Daemon.php new file mode 100644 index 0000000..3a19af5 --- /dev/null +++ b/src/Daemon.php @@ -0,0 +1,51 @@ + $content"); + fclose($file); + return intval($content); +} + +function daemon(array $info): void { + $pid = getPid($info['pidFile']); + if ($pid == -1 or !isPid($pid)) { // pid not found + logging::warning('Catch ' . $info['name'] . ' exit'); + new Process($info['command']); + logging::info('Restart ' . $info['name'] . ' success'); + } +} diff --git a/src/demo.php b/src/demo.php index ffa565e..67c2662 100644 --- a/src/demo.php +++ b/src/demo.php @@ -1,55 +1,36 @@ 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( + 'name' => 'nginx', '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); -} +$phpFpm = array( + 'name' => 'php-fpm8', + 'command' => ['/usr/sbin/php-fpm8'], + 'pidFile' => '/run/php-fpm8.pid', +); + +$vlmcsd = array( + 'name' => 'vlmcsd', + 'command' => ['/usr/bin/vlmcsd', '-e', '-p', '/run/vlmcsd.pid'], + 'pidFile' => '/run/vlmcsd.pid', +); + +declare(ticks = 1); +pcntl_signal(SIGCHLD, 'subExit'); // receive SIGCHLD signal -$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); +new Process($nginx['command']); +new Process($phpFpm['command']); +new Process($vlmcsd['command']); +while (true) { + msSleep(3000); // sleep 3s + daemon($nginx); + daemon($phpFpm); + daemon($vlmcsd); }