NonStopableProcess.php 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Runs a PHP script that can be stopped only with a SIGKILL (9) signal for 3 seconds.
  12. *
  13. * @args duration Run this script with a custom duration
  14. *
  15. * @example `php NonStopableProcess.php 42` will run the script for 42 seconds
  16. */
  17. function handleSignal($signal)
  18. {
  19. $name = match ($signal) {
  20. \SIGTERM => 'SIGTERM',
  21. \SIGINT => 'SIGINT',
  22. default => $signal.' (unknown)',
  23. };
  24. echo "signal $name\n";
  25. }
  26. pcntl_signal(\SIGTERM, 'handleSignal');
  27. pcntl_signal(\SIGINT, 'handleSignal');
  28. echo 'received ';
  29. $duration = isset($argv[1]) ? (int) $argv[1] : 3;
  30. $start = microtime(true);
  31. while ($duration > (microtime(true) - $start)) {
  32. usleep(10000);
  33. pcntl_signal_dispatch();
  34. }