dongzhi4470 2015-11-27 10:35
浏览 77

如何在php-fpm / nginx上停止php脚本执行?

Unfortunately calling php exit() on php-fpm/nginx configuration does not stop the script immediately while file handles might not be closed.

Some developers suggest calling fastcgi_finish_request() but also this does not stop the script.

Others suggest wrapping all code in a catch block:

    class SystemExit extends Exception {}
    try {
       /* PUT ALL THE CODE HERE */
    } catch (SystemExit $e) { /* do nothing */ }

and throwing an exception where code stop is needed:

   throw new SystemExit(); // instead of exit()

This would mean editing all php files to include try/catch block and seems tedious to me.

Are there any other clean solutions?

  • 写回答

1条回答 默认 最新

  • drbd65446 2015-11-27 11:33

    So we found out that it's a register_shutdown_function callback that prevents your script from exiting immediately.

    PHP shutdown function is designed to be called on any script shutdown when possible. But it has a feature: if one of shutdown callbacks calls exit — script is exiting without calling any other callbacks.

    So if you really want to skip a shutdown function in some cases, you should register some killer-function as a very first shutdown callback. Inside that killer-function you will check a kind of singleton for state: do we want to exit? — call exit(), otherwise — return.

    function killer_function() {
        if ($someGlobalThing->needToExitRightNow()) {
    // ...
    function exit_now() {

    ($someGlobalThing can be a singleton or some super-global variable or a global registry or whatever you like)

    Then calling exit_now() will do the trick.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 matlab runtime 如何收费
    • ¥20 用C语言绘制cs1.6方框透视 出现了点问题 绘制不上去 矩阵数据 和敌人坐标都是正确的
    • ¥15 Tpad api账户 api口令
    • ¥30 ppt进度条制作,vba语言
    • ¥15 生信simpleaffy包下载
    • ¥15 请教一下simulink中S函数相关问题
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)