mirror of https://github.com/dnomd343/kms-server
dnomd343
2 years ago
2 changed files with 76 additions and 44 deletions
@ -0,0 +1,51 @@ |
|||
<?php |
|||
|
|||
require_once 'Logger.php'; |
|||
require_once 'Process.php'; |
|||
|
|||
function subExit(): void { |
|||
pcntl_wait($status); // avoid zombie process |
|||
} |
|||
|
|||
function msSleep(int $ms): void { // sleep for xxx ms |
|||
for ($i = 0; $i < $ms; $i++) { |
|||
usleep(1000); // split multiple times (avoid SIGCHLD signal) |
|||
} |
|||
} |
|||
|
|||
function isPid(int $pid): bool { |
|||
$raw = explode(PHP_EOL, shell_exec('ps -ao pid')); // get pid list |
|||
array_shift($raw); // remove output caption |
|||
foreach ($raw as $row) { |
|||
$row = trim($row); |
|||
if (!$row == '' and intval($row) == $pid) { // target pid exist |
|||
return true; |
|||
} |
|||
} |
|||
return false; // pid not found |
|||
} |
|||
|
|||
function getPid(string $pidFile): int { // get pid by given file |
|||
if (!file_exists($pidFile)) { // file not exist |
|||
logging::warning("PID file $pidFile not exist"); |
|||
return -1; |
|||
} |
|||
$file = fopen($pidFile, 'r'); |
|||
if (!is_resource($file)) { // file open failed |
|||
logging::warning("Couldn't open PID file $pidFile"); |
|||
return -1; |
|||
} |
|||
$content = trim(fread($file, filesize($pidFile))); // read pid number |
|||
logging::debug("Get PID from $pidFile -> $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'); |
|||
} |
|||
} |
@ -1,55 +1,36 @@ |
|||
<?php |
|||
|
|||
//require_once 'Logger.php'; |
|||
// |
|||
//logging::debug('debug'); |
|||
//logging::info('info'); |
|||
//logging::warning('warning'); |
|||
//logging::error('error'); |
|||
//logging::critical('critical'); |
|||
|
|||
require_once 'Daemon.php'; |
|||
require_once 'Logger.php'; |
|||
require_once 'Process.php'; |
|||
|
|||
//$vlmcsd = new Process(['/usr/bin/vlmcsd', '-De'], $capture = false); |
|||
//var_dump($vlmcsd); |
|||
//echo $vlmcsd->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', |
|||
); |
|||
|
|||
$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); |
|||
$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 |
|||
|
|||
new Process($nginx['command']); |
|||
new Process($phpFpm['command']); |
|||
new Process($vlmcsd['command']); |
|||
while (true) { |
|||
msSleep(3000); // sleep 3s |
|||
daemon($nginx); |
|||
daemon($phpFpm); |
|||
daemon($vlmcsd); |
|||
} |
|||
|
Loading…
Reference in new issue