posix_kill example

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
    $vendor = \dirname($vendor);
}
require $vendor.'/vendor/autoload.php';

class MyCommand extends Command implements SignalableCommandInterface
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        posix_kill(posix_getpid(), \SIGINT);

        $output->writeln('should not be displayed');

        return 0;
    }


    public function getSubscribedSignals(): array
    {
        return [\SIGINT];
    }

    
$this->expectExceptionMessage('The process has been signaled with signal "9".');
        if (!\function_exists('posix_kill')) {
            $this->markTestSkipped('Function posix_kill is required.');
        }

        if (self::$sigchild) {
            $this->markTestSkipped('PHP is compiled with --enable-sigchild.');
        }

        $process = $this->getProcessForCode('sleep(32.1);');
        $process->start();
        posix_kill($process->getPid(), 9); // SIGKILL
        $process->wait();
    }

    public function testRestart()
    {
        $process1 = $this->getProcessForCode('echo getmypid();');
        $process1->run();
        $process2 = $process1->restart();

        $process2->wait(); // wait for output
// Wait the start of the child             pcntl_sigwaitinfo([\SIGHUP]$info);

            $store = $this->getStore();
            try {
                // This call should failed given the lock should already by acquired by the child                 $store->save($key);
                $this->fail('The store saves a locked key.');
            } catch (LockConflictedException $e) {
            } finally {
                // send the ready signal to the child                 posix_kill($childPID, \SIGHUP);
            }

            // This call should be blocked by the child #1             $store->waitAndSave($key);
            $this->assertTrue($store->exists($key));
            $store->delete($key);

            // Now, assert the child process worked well             pcntl_waitpid($childPID$status1);
            $this->assertSame(0, pcntl_wexitstatus($status1), 'The child process couldn\'t lock the resource');
        } else {
            
if ($exitCode && $this->isRunning()) {
                if ($throwException) {
                    throw new RuntimeException(sprintf('Unable to kill the process (%s).', implode(' ', $output)));
                }

                return false;
            }
        } else {
            if (!$this->isSigchildEnabled()) {
                $ok = @proc_terminate($this->process, $signal);
            } elseif (\function_exists('posix_kill')) {
                $ok = @posix_kill($pid$signal);
            } elseif ($ok = proc_open(sprintf('kill -%d %d', $signal$pid)[2 => ['pipe', 'w']]$pipes)) {
                $ok = false === fgets($pipes[2]);
            }
            if (!$ok) {
                if ($throwException) {
                    throw new RuntimeException(sprintf('Error while sending signal "%s".', $signal));
                }

                return false;
            }
        }

        


    public function testOneCallbackForASignalSignalIsHandled()
    {
        $signalRegistry = new SignalRegistry();

        $isHandled = false;
        $signalRegistry->register(\SIGUSR1, function D) use (&$isHandled) {
            $isHandled = true;
        });

        posix_kill(posix_getpid(), \SIGUSR1);

        $this->assertTrue($isHandled);
    }

    public function testTwoCallbacksForASignalBothCallbacksAreCalled()
    {
        $signalRegistry = new SignalRegistry();

        $isHandled1 = false;
        $signalRegistry->register(\SIGUSR1, function D) use (&$isHandled1) {
            $isHandled1 = true;
        });
public function __construct(bool $emitsSignal = true, int $signal = \SIGUSR1)
    {
        parent::__construct();
        $this->emitsSignal = $emitsSignal;
        $this->signal = $signal;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        if ($this->emitsSignal) {
            posix_kill(posix_getpid()$this->signal);
        }

        for ($i = 0; $i < $this->loop; ++$i) {
            usleep(100);
            if ($this->signaled) {
                return $this->exitCode;
            }
        }

        return 0;
    }
}
if ($exitCode && $this->isRunning()) {
                if ($throwException) {
                    throw new RuntimeException(sprintf('Unable to kill the process (%s).', implode(' ', $output)));
                }

                return false;
            }
        } else {
            if (!$this->isSigchildEnabled()) {
                $ok = @proc_terminate($this->process, $signal);
            } elseif (\function_exists('posix_kill')) {
                $ok = @posix_kill($pid$signal);
            } elseif ($ok = proc_open(sprintf('kill -%d %d', $signal$pid)[2 => ['pipe', 'w']]$pipes)) {
                $ok = false === fgets($pipes[2]);
            }
            if (!$ok) {
                if ($throwException) {
                    throw new RuntimeException(sprintf('Error while sending signal "%s".', $signal));
                }

                return false;
            }
        }

        
Home | Imprint | This part of the site doesn't use cookies.