Last active
November 25, 2019 22:53
-
-
Save yaredc/50f90d050f5e4244312a7c48fc55ec5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
declare(strict_types=1); | |
declare(ticks=1); | |
use Common\Helper\Logger; | |
if (PHP_SAPI !== 'cli') { | |
exit(); | |
} | |
require dirname(__DIR__) . '/vendor/autoload.php'; | |
Config::init(Config::CONTEXT_DAEMON); | |
$pid = pcntl_fork(); | |
if ($pid === -1) { | |
Logger::log()->error('Could not fork.'); | |
exit(1); | |
} | |
if ($pid) { | |
pcntl_wait($status); // PROTECT AGAINST ZOMBIE CHILDREN | |
} | |
$signalHandler = static function (int $signal): void { | |
Logger::log()->info(sprintf('Received signal %d', $signal)); | |
switch ($signal) { | |
case SIGTERM; | |
case SIGHUP; | |
exit(0); | |
break; | |
} | |
}; | |
pcntl_signal(SIGTERM, $signalHandler); | |
pcntl_signal(SIGHUP, $signalHandler); | |
// MAIN LOOP | |
while (true) { | |
sleep(2); | |
//DO STUFF | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment