我正在从 CLI 运行 php 脚本,并且我想在脚本用 ctrl + c 停止时执行一个函数。我试过这个:
<?php
declare(ticks = 1);
function sigint() {
echo 'This is the end';
exit;
}
pcntl_signal(SIGTERM, 'sigint');
$i = 1;
do {
echo $i++ . ' ';
sleep(1);
} while (TRUE);
但它不起作用。我怎样才能做到这一点?