php - 退出时自动重启 PHP 脚本

标签 php bash cron sh restart

有没有一种方法可以让我在 PHP 脚本退出时自动重启它,而不管它是否已正确退出,或是否因错误或内存使用量上限等而终止?

最佳答案

PHP 脚本也可以使用 PCNTL 重新启动。

免责声明:本练习的目的只是为了证明 PHP 完全有能力重新启动自己并回答问题:

Is there a way I can automatically restart a PHP script whenever it exits, regardless of whether it has been exited properly, or has terminated due to an error?

因此,我们无法详细介绍有关 unix 进程的任何细节,因此我建议您从 PCNTL book 开始。或引用 更多细节。需要开发人员的自由裁量权,仅仅因为我们可以,并不意味着它是明智的。抛开所有的严肃性,让我们玩得开心。

在示例中,我们假设它是在 *nix 环境中从带有半体面 shell 的终端使用以下命令启动的 PHP CLI 脚本:

$ php i-can-restart-myself.php 0

我们传递一个重启计数属性作为进程重启的指示器。

我可以自动重启 PHP 脚本吗?

是的,我可以!

<?php
    echo ++$argv[1];     // count every restart
    $_ = $_SERVER['_'];  // or full path to php binary

    echo "\n======== start =========\n";
    // do a lot of stuff
    $cnt = 0;
    while( $cnt++ < 10000000 ){}
    echo "\n========== end =========\n";
    
    // restart myself
    pcntl_exec($_, $argv);

不管是否已经正常终止?

是的,如果终止,我可以重新启动!

<?php
    echo ++$argv[1];    
    $_ = $_SERVER['_']; 
    
    register_shutdown_function(function () {
        global $_, $argv; // note we need to reference globals inside a function
        // restart myself
        pcntl_exec($_, $argv);
    });

    echo "\n======== start =========\n";
    // do a lot of stuff
    $cnt = 0;
    while( $cnt++ < 10000000 ){}
    echo "\n========== end =========\n";
    
    die; // exited properly
    // we can't reach here 
    pcntl_exec($_, $argv);

还是因错误而终止?

同上!

<?php
    echo ++$argv[1];    
    $_ = $_SERVER['_']; 
    
    register_shutdown_function(function () {
        global $_, $argv; 
        // restart myself
        pcntl_exec($_, $argv);
    });

    echo "\n======== start =========\n";
    // do a lot of stuff
    $cnt = 0;
    while( $cnt++ < 10000000 ){}
    echo "\n===== what if? =========\n";
    require 'OOPS! I dont exist.'; // FATAL Error:

    // we can't reach here
    echo "\n========== end =========\n";        
    die; // exited properly
    // we can't reach here 
    pcntl_exec($_, $argv);

但你知道我想要的不止这些,对吧?

当然!我可以在信号 kill、hub 甚至 Ctrl-C 上重新启动!

<?php
    echo ++$argv[1];     
    $_ = $_SERVER['_'];  
    $restartMyself = function () {
        global $_, $argv; 
        pcntl_exec($_, $argv);
    };
    register_shutdown_function($restartMyself);
    pcntl_signal(SIGTERM, $restartMyself); // kill
    pcntl_signal(SIGHUP,  $restartMyself); // kill -s HUP or kill -1
    pcntl_signal(SIGINT,  $restartMyself); // Ctrl-C

    echo "\n======== start =========\n";
    // do a lot of stuff
    $cnt = 0;
    while( $cnt++ < 10000000 ){}
    echo "\n===== what if? =========\n";
    require 'OOPS! I dont exist.'; // FATAL Error:

    // we can't reach here
    echo "\n========== end =========\n";        
    die; // exited properly
    // we can't reach here 
    pcntl_exec($_, $argv);

我现在如何终止它?

如果您通过按住 Ctrl-C 来淹没进程,您可能会在关机的某个地方捕获它。

我不想看到所有这些错误,我也可以重新开始吗?

没问题我也能处理错误!

<?php
    echo ++$argv[1];     
    $_ = $_SERVER['_'];  
    $restartMyself = function () {
        global $_, $argv; 
        pcntl_exec($_, $argv);
        // NOTE: consider fork and exiting here as error handler
    };
    register_shutdown_function($restartMyself);
    pcntl_signal(SIGTERM, $restartMyself);   
    pcntl_signal(SIGHUP,  $restartMyself);   
    pcntl_signal(SIGINT,  $restartMyself);   
    set_error_handler($restartMyself , E_ALL); // Catch all errors

    echo "\n======== start =========\n";
    // do a lot of stuff
    $cnt = 0;
    while( $cnt++ < 10000000 ){}

    echo $CAREFUL_NOW; // NOTICE: will also be caught

    // we would normally still go here
    echo "\n===== what if? =========\n";
    require 'OOPS! I dont exist.'; // FATAL Error:

    // we can't reach here
    echo "\n========== end =========\n";        
    die; // exited properly
    // we can't reach here 
    pcntl_exec($_, $argv);

虽然乍一看这似乎工作正常,因为 pcntl_exec 在同一个进程中运行,我们没有注意到事情非常错误。如果您想生成一个新进程并让旧进程死掉,这是一个完全可行的替代方案 see next post ,您会发现每次迭代都会启动 2 个进程,因为我们触发了一个 PHP NOTICE 和一个由于共同疏忽而导致的 ERROR。这可以通过确保在调用 pcntl_exec 之后确保我们 die()exit() 来轻松纠正,因为实现错误处理程序 PHP 假定具有容错性被接受并继续运行。收窄 the error reporting level而不是捕获 E_ALL 也会更负责任。

我要说明的一点是,即使您能够重新启动失败的脚本,这也不是允许代码损坏的借口。尽管这种做法可能存在可行的用例,但我强烈反对将失败时重新启 Action 为脚本因错误而失败的解决方案! 正如我们从这些示例无法确切知道它失败的位置,因此我们无法确定它将从哪里重新开始。寻求看似工作正常的“快速修复”解决方案现在可能比以前遇到更多问题。

我更愿意通过一些适当的单元测试来解决缺陷,这将有助于排除罪魁祸首,以便我们可以纠正问题。如果 PHP 内存不足,可以通过在使用后取消设置变量来保守地使用资源来避免。 (顺便说一句。你会发现分配 null 比使用 unset 获得相同的结果要快得多)但我担心,如果不解决,重新启动肯定会成为你已经拥有的蠕虫 jar 的合适开启者。

关于php - 退出时自动重启 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798438/

相关文章:

linux - 如何: compare command output to text file with bash

bash 确定变量是否为空,如果是则退出。

php - PHP逻辑每日更新

bash - docker exec 在 cron 中不工作

linux - 无法连续运行相同的作业

php - 将登录或注册链接移动到标题导航中的顶部链接 - Magento

python - 构建一个 conda 包,从 env.yaml 文件创建一个 conda 环境

php - 操作数据库的正确方法

javascript - 包含 PHP 文件的 jquery div 刷新

php - 将 Angular 4 与 LARAVEL 5.4 连接起来