在 CLI 上的脚本中止后执行代码
Execute code after script abort on CLI
我的脚本在 PHP 中止后,我尝试执行一些最终代码。假设我有这个 PHP 脚本:
while(true) {
echo 'loop';
sleep(1);
}
如果我用 $ php script.php
执行脚本,它会 运行 到给定的执行时间。
现在我喜欢在脚本中止后执行一些最终代码。所以如果我
- 命中
Ctrl+C
- 执行时间结束
在这些情况下是否有可能进行一些清理?
我用 pcntl_signal
but with no luck. Also with register_shutdown_function
尝试过,但只有在脚本成功结束时才会调用它。
更新
我发现(感谢 rch 的 link)我可以通过以下方式 "catch" 事件:
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
闭包中的代码
这可能有一些非常有用的信息,看起来他们正在使用与您尝试过的相同的东西,但似乎取得了积极的结果?也许这里有些东西你没有尝试过或者可能错过了。
Automatically Restart PHP Script on Exit
检查这个:connection_aborted()
http://php.net/manual/en/function.connection-aborted.php
下面是一个如何使用它来实现你想要的东西的例子:
http://php.net/manual/en/function.connection-aborted.php#111167
函数 pcntl_signal()
是脚本被使用 Ctrl-C
(和其他信号)中断的情况的答案。您必须注意文档。它说:
You must use the declare()
statement to specify the locations in your program where callbacks are allowed to occur for the signal handler to function properly.
除其他外,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);
}
我的脚本在 PHP 中止后,我尝试执行一些最终代码。假设我有这个 PHP 脚本:
while(true) {
echo 'loop';
sleep(1);
}
如果我用 $ php script.php
执行脚本,它会 运行 到给定的执行时间。
现在我喜欢在脚本中止后执行一些最终代码。所以如果我
- 命中
Ctrl+C
- 执行时间结束
在这些情况下是否有可能进行一些清理?
我用 pcntl_signal
but with no luck. Also with register_shutdown_function
尝试过,但只有在脚本成功结束时才会调用它。
更新
我发现(感谢 rch 的 link)我可以通过以下方式 "catch" 事件:
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
闭包中的代码
这可能有一些非常有用的信息,看起来他们正在使用与您尝试过的相同的东西,但似乎取得了积极的结果?也许这里有些东西你没有尝试过或者可能错过了。
Automatically Restart PHP Script on Exit
检查这个:connection_aborted()
http://php.net/manual/en/function.connection-aborted.php
下面是一个如何使用它来实现你想要的东西的例子:
http://php.net/manual/en/function.connection-aborted.php#111167
函数 pcntl_signal()
是脚本被使用 Ctrl-C
(和其他信号)中断的情况的答案。您必须注意文档。它说:
You must use the
declare()
statement to specify the locations in your program where callbacks are allowed to occur for the signal handler to function properly.
除其他外,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);
}