Answer the question
In order to leave comments, you need to log in
Running daemons from under the Framework, what should I do?
Hello, I've run into the following problem. For my project, I need to create a PHP daemon that will perform certain operations in the background, but I ran into the following problem. If I call the script from under Bitrix through the admin panel, a process is created ( puu.sh/uvxga/13b12c7570.png) which cannot be reached, it simply ignores the signals that are sent to it either through the admin panel (via the PHP command line or or through the launch of the script), or from under the console under the root (For example, Kill -SIGTERM ID), even the Kill command is not able to kill him. The launched script from the CentOS console, on the contrary, responds well to the signals sent.
PHP daemon code:
<?php
/**
* @link
*/
declare(ticks = 1);
$stop = FALSE;
/**
* Функция перехватывающая сигналы
*/
function sig_handler($signo)
{
global $stop;
switch ($signo)
{
case SIGTERM:
$stop = TRUE;
break;
case SIGUSR1:
break;
default:
// Ловим все остальные сигналы
}
}
// Регистрируем сигналы
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");
// Форкаем процесс
$pid = pcntl_fork();
echo $pid.PHP_EOL;
if ($pid == -1)
{
// Ошибка
die('could not fork'.PHP_EOL);
}
else if ($pid)
{
// Родительский процесс, убиваем
die('die parent process'.PHP_EOL);
}
else
{
// Новый процесс, запускаем главный цикл
while( ! $stop )
{
sleep(1);
pcntl_signal_dispatch();
// полезная работа
}
}
// Отцепляемся от терминала
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question