【问题标题】:Execute code after script abort on CLI在 CLI 上中止脚本后执行代码
【发布时间】:2015-09-05 02:50:12
【问题描述】:

我的脚本在 PHP 中中止后,我尝试执行一些最终代码。所以假设我有这个 PHP 脚本:

while(true) {
    echo 'loop';
    sleep(1);
}

如果我使用$ php script.php 执行脚本,它会一直运行到给定的执行时间。

现在我想在脚本中止后执行一些最终代码。所以如果我

  • 点击Ctrl+C
  • 执行时间结束

在这些情况下是否有可能进行一些清理?

我用pcntl_signal 尝试过,但没有运气。也可以使用register_shutdown_function,但只有在脚本成功结束时才会调用它。

更新

我发现(感谢 rch 的链接)我可以通过以下方式“捕捉”事件:

pcntl_signal(SIGTERM, $restartMyself); // kill
pcntl_signal(SIGHUP,  $restartMyself); // kill -s HUP or kill -1
pcntl_signal(SIGINT,  $restartMyself); // Ctrl-C

但如果我用

扩展我的代码
$cleanUp = function() {
    echo 'clean up';
    exit;
};

pcntl_signal(SIGINT, $cleanUp);

如果我点击Ctrl+C,脚本会继续执行,但不尊重$cleanUp 闭包中的代码。

【问题讨论】:

    标签: php command-line-interface


    【解决方案1】:

    函数pcntl_signal() 是当脚本被使用Ctrl-C(和其他信号)中断的情况的答案。您必须注意文档。它说:

    您必须使用declare() 语句来指定程序中允许发生回调的位置,以便信号处理程序正常运行。

    declare() 语句安装一个回调函数,通过调用函数pcntl_signal_dispatch() 来处理自上次调用以来接收到的信号的调度,该函数又调用您安装的信号处理程序。

    或者,当您认为函数pcntl_signal_dispatch() 适合您的代码流时,您可以自己调用它(并且根本不要使用declare(ticks=1))。

    这是一个使用declare(ticks=1)的示例程序:

    declare(ticks=1);
    
    // Install the signal handlers
    pcntl_signal(SIGHUP,  'handleSigHup');
    pcntl_signal(SIGINT,  'handleSigInt');
    pcntl_signal(SIGTERM, 'handleSigTerm');
    
    
    while(true) {
        echo 'loop';
        sleep(1);
    }
    
    // Reset the signal handlers
    pcntl_signal(SIGHUP,  SIG_DFL);
    pcntl_signal(SIGINT,  SIG_DFL);
    pcntl_signal(SIGTERM, SIG_DFL);
    
    
    
    /**
     * SIGHUP: the controlling pseudo or virtual terminal has been closed
     */
    function handleSigHup()
    {
        echo("Caught SIGHUP, terminating.\n");
        exit(1);
    }
    
    /**
     * SIGINT: the user wishes to interrupt the process; this is typically initiated by pressing Control-C
     *
     * It should be noted that SIGINT is nearly identical to SIGTERM.
     */
    function handleSigInt()
    {
        echo("Caught SIGINT, terminating.\n");
        exit(1);
    }
    
    /**
     * SIGTERM: request process termination
     *
     * The SIGTERM signal is a generic signal used to cause program termination.
     * It is the normal way to politely ask a program to terminate.
     * The shell command kill generates SIGTERM by default.
     */
    function handleSigTerm()
    {
        echo("Caught SIGTERM, terminating.\n");
        exit(1);
    }
    

    【讨论】:

    • 谢谢!奇迹般有效。你能告诉我为什么我必须重置信号处理程序吗?我认为那是我在使用 pcntl_signal 和ticks = 1 的方法中错过的东西...
    • 我不知道重置信号处理程序是否有效。我把它们放在那里是出于习惯,关闭打开的文件,破坏构造的对象,释放分配的资源是一种很好的编码习惯。如果您不想立即exit() 但想要进行额外处理,建议在信号处理程序中设置一个标志并在主程序循环中检查它并相应地继续(或使用pcntl_signal_dispatch() 而不是declare(ticks=1)。你可以阅读更多关于termination signals的信息。
    • 再次感谢答案和有趣的链接。因此,如果我喜欢使用pcntl_signal_dispatch 而不是ticks = 1,我必须在哪里调用它?在我的循环中的某个地方?然后脚本被执行到我猜的调度调用,然后调用pcntl_signal函数定义的回调。对吗?
    • PHP 为信号安装处理程序,并将接收到的信号放入队列中。函数pcntl_signal_dispatch() 从队列中挑选信号并调用您使用pcntl_signal() 为它们提供的处理程序。它在您调用它时运行(每个循环一次或每当它更适合您的代码时)。语句declare(ticks=1) 安装了一个调用pcntl_signal_dispatch() 的tick function。由于ticks=1,它运行得更频繁,脚本响应更快(但运行速度稍慢)。
    【解决方案2】:

    检查这个:connection_aborted()

    http://php.net/manual/en/function.connection-aborted.php

    这是一个如何使用它来实现您想要的示例:

    http://php.net/manual/en/function.connection-aborted.php#111167

    【讨论】:

      【解决方案3】:

      这可能包含一些非常有用的信息,看起来他们正在使用您尝试过的相同方法,但似乎取得了积极的结果?也许这里有一些你没有尝试或错过的东西。

      Automatically Restart PHP Script on Exit

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-01
        • 2018-10-27
        • 1970-01-01
        • 2013-08-09
        • 1970-01-01
        • 2018-03-21
        • 1970-01-01
        • 2019-11-29
        相关资源
        最近更新 更多