NonStopableProcess.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. switch ($signal) {
  20. case SIGTERM:
  21. $name = 'SIGTERM';
  22. break;
  23. case SIGINT:
  24. $name = 'SIGINT';
  25. break;
  26. default:
  27. $name = $signal.' (unknown)';
  28. break;
  29. }
  30. echo "signal $name\n";
  31. }
  32. pcntl_signal(SIGTERM, 'handleSignal');
  33. pcntl_signal(SIGINT, 'handleSignal');
  34. echo 'received ';
  35. $duration = isset($argv[1]) ? (int) $argv[1] : 3;
  36. $start = microtime(true);
  37. while ($duration > (microtime(true) - $start)) {
  38. usleep(10000);
  39. pcntl_signal_dispatch();
  40. }