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 |
<?php |
||||
|
|
||||
//require_once 'Logger.php'; |
require_once 'Daemon.php'; |
||||
// |
require_once 'Logger.php'; |
||||
//logging::debug('debug'); |
|
||||
//logging::info('info'); |
|
||||
//logging::warning('warning'); |
|
||||
//logging::error('error'); |
|
||||
//logging::critical('critical'); |
|
||||
|
|
||||
require_once 'Process.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( |
$nginx = array( |
||||
|
'name' => 'nginx', |
||||
'command' => ['/usr/sbin/nginx'], |
'command' => ['/usr/sbin/nginx'], |
||||
'pidFile' => '/run/nginx/nginx.pid', |
'pidFile' => '/run/nginx/nginx.pid', |
||||
); |
); |
||||
|
|
||||
function getPid(string $pidFile): int { // get pid by given file |
$phpFpm = array( |
||||
if (!file_exists($pidFile)) { |
'name' => 'php-fpm8', |
||||
return -1; // file not exist |
'command' => ['/usr/sbin/php-fpm8'], |
||||
} |
'pidFile' => '/run/php-fpm8.pid', |
||||
$file = fopen($pidFile, 'r'); |
); |
||||
if (!is_resource($file)) { |
|
||||
return -1; // file open failed |
$vlmcsd = array( |
||||
} |
'name' => 'vlmcsd', |
||||
$content = fread($file, filesize($pidFile)); // read pid number |
'command' => ['/usr/bin/vlmcsd', '-e', '-p', '/run/vlmcsd.pid'], |
||||
fclose($file); |
'pidFile' => '/run/vlmcsd.pid', |
||||
return intval($content); |
); |
||||
} |
|
||||
|
declare(ticks = 1); |
||||
|
pcntl_signal(SIGCHLD, 'subExit'); // receive SIGCHLD signal |
||||
|
|
||||
$p = new Process($nginx['command']); |
new Process($nginx['command']); |
||||
sleep(1); |
new Process($phpFpm['command']); |
||||
while (True) { |
new Process($vlmcsd['command']); |
||||
if (getPid($nginx['pidFile']) == -1) { |
while (true) { |
||||
echo 'nginx exit' . PHP_EOL; |
msSleep(3000); // sleep 3s |
||||
$p = new Process($nginx['command']); |
daemon($nginx); |
||||
} |
daemon($phpFpm); |
||||
$p->status(); |
daemon($vlmcsd); |
||||
sleep(1); |
|
||||
} |
} |
||||
|
Loading…
Reference in new issue