Php trapping maximum execution time error

淺唱寂寞╮ 提交于 2019-12-03 11:22:54

Try setting a "Done" flag after the execution of a file and registering a shutdown function that will check if that flag is defined

After reading the first two answers here, I had to test register_shutdown_function() myself -- I didn't think it'd run. After all, how can a user function run once there's no more memory, or execution time has elapsed? I was surprised to learn that shutdown functions do run, even in an OOM situation, or when execution time has been exceded. Proof of concept:

To test memory limit:

<?php
function asdf() { echo "omg\n"; }
register_shutdown_function('asdf');

ini_set('memory_limit', '1000');

$x = '';
while(true) {
 $x .= 'lkajsdlfkjasldkfjlaskdfjasldkfj';
}

Output:

PHP Fatal error:  Allowed memory size of 262144 bytes exhausted (tried to allocate 169540 bytes) in /home/scratch.php on line 9
PHP Stack trace:
PHP   1. {main}() /home/scratch.php:0

Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 169540 bytes) in /home/scratch.php on line 9

Call Stack:
    0.0002      81360   1. {main}() /home/scratch.php:0

omg

To test execution time:

cat scratch.php
<?php
function asdf() { echo "omg\n"; }
register_shutdown_function('asdf');

set_time_limit(1);

while(true) {}

Output:

PHP Fatal error:  Maximum execution time of 1 second exceeded in /home/scratch.php on line 7
PHP Stack trace:
PHP   1. {main}() /home/scratch.php:0

Fatal error: Maximum execution time of 1 second exceeded in /home/scratch.php on line 7

Call Stack:
    0.0002      80200   1. {main}() /home/scratch.php:0

omg

Note that, to get your message to display before PHP's error output, you'd have to disable PHP's error output entirely. Which is best practice for a production site anyway.

My original answer won't work, as you can't catch a fatal error. If you want to read it anyway, check the revision history.

My only guess is that you could use register_shutdown_function. Set a variable at the start of the script, clear it when the script has completed successfully. Write a function that checks that variable and acts on it if it's still set, then apply the function using register_shutdown_function

http://php.net/manual/en/function.register-shutdown-function.php

If there's no way to trap the error from the API level, the PHP engine should be able to dump the memory on hdd whenever a fatal error occurs, like what Windows does when you see a 'blue screen'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!