Browse Source

update: process daemon

master
dnomd343 2 years ago
parent
commit
163a706bcc
  1. 51
      src/Daemon.php
  2. 69
      src/demo.php

51
src/Daemon.php

@ -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');
}
}

69
src/demo.php

@ -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
}
$content = fread($file, filesize($pidFile)); // read pid number
fclose($file);
return intval($content);
}
$p = new Process($nginx['command']); $vlmcsd = array(
sleep(1); 'name' => 'vlmcsd',
while (True) { 'command' => ['/usr/bin/vlmcsd', '-e', '-p', '/run/vlmcsd.pid'],
if (getPid($nginx['pidFile']) == -1) { 'pidFile' => '/run/vlmcsd.pid',
echo 'nginx exit' . PHP_EOL; );
$p = new Process($nginx['command']);
} declare(ticks = 1);
$p->status(); pcntl_signal(SIGCHLD, 'subExit'); // receive SIGCHLD signal
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);
} }

Loading…
Cancel
Save