Browse Source

feat: demo of nginx daemon

master
dnomd343 2 years ago
parent
commit
b6513c5e1b
  1. 50
      src/Process.php
  2. 58
      src/demo.php

50
src/Process.php

@ -42,8 +42,12 @@ class Process {
proc_terminate($this->process, $signal); 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 public function isAlive(): bool { // whether sub process still running
return proc_get_status($this->process)['running']; return $this->status()['running'];
} }
public function quit(): int { public function quit(): int {
@ -54,25 +58,25 @@ class Process {
} }
$p = new Process(['sleep', '10'], $capture = true); //$p = new Process(['sleep', '10'], $capture = true);
echo "PID -> $p->pid\n"; //echo "PID -> $p->pid\n";
//
echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; //echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n";
echo "Sleep 5s...\n"; //echo "Sleep 5s...\n";
sleep(5); //sleep(5);
//
echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; //echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n";
echo "Send kill signal\n"; //echo "Send kill signal\n";
$p->signal(15); //$p->signal(15);
sleep(1); //sleep(1);
echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n"; //echo "Alive -> " . ($p->isAlive() ? 'yes' : 'no') . "\n";
//
echo '--------------------------------------------' . PHP_EOL; //echo '--------------------------------------------' . PHP_EOL;
echo $p->getStdout(); //echo $p->getStdout();
echo '--------------------------------------------' . PHP_EOL; //echo '--------------------------------------------' . PHP_EOL;
echo $p->getStdout(); //echo $p->getStdout();
echo '--------------------------------------------' . PHP_EOL; //echo '--------------------------------------------' . PHP_EOL;
echo $p->getStderr(); //echo $p->getStderr();
echo '--------------------------------------------' . PHP_EOL; //echo '--------------------------------------------' . PHP_EOL;
//
echo "Return code -> " . $p->quit() . "\n"; //echo "Return code -> " . $p->quit() . "\n";

58
src/demo.php

@ -1,9 +1,55 @@
<?php <?php
require_once 'Logger.php'; //require_once 'Logger.php';
//
//logging::debug('debug');
//logging::info('info');
//logging::warning('warning');
//logging::error('error');
//logging::critical('critical');
logging::debug('debug'); require_once 'Process.php';
logging::info('info');
logging::warning('warning'); //$vlmcsd = new Process(['/usr/bin/vlmcsd', '-De'], $capture = false);
logging::error('error'); //var_dump($vlmcsd);
logging::critical('critical'); //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(
'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);
}

Loading…
Cancel
Save