NonStopableProcess.php 810 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * Runs a PHP script that can be stopped only with a SIGKILL (9) signal for 3 seconds
  4. *
  5. * @args duration Run this script with a custom duration
  6. *
  7. * @example `php NonStopableProcess.php 42` will run the script for 42 seconds
  8. */
  9. function handleSignal($signal)
  10. {
  11. switch ($signal) {
  12. case SIGTERM:
  13. $name = 'SIGTERM';
  14. break;
  15. case SIGINT:
  16. $name = 'SIGINT';
  17. break;
  18. default:
  19. $name = $signal . ' (unknown)';
  20. break;
  21. }
  22. echo "received signal $name\n";
  23. }
  24. declare(ticks=1);
  25. pcntl_signal(SIGTERM, 'handleSignal');
  26. pcntl_signal(SIGINT, 'handleSignal');
  27. $duration = isset($argv[1]) ? (int) $argv[1] : 3;
  28. $start = microtime(true);
  29. while ($duration > (microtime(true) - $start)) {
  30. usleep(1000);
  31. }