Ctrl+C sends SIGINT which by default terminates an application or script immediately. If you build a catch for that, you can have your script end more cleanly.
<?php
declare(ticks = 1);
pcntl_signal(SIGTERM, "sig_handle");
pcntl_signal(SIGINT, "sig_handle");
$TERMINATE = false;
function sig_handle($signal)
{
switch($signal)
{
case SIGTERM:
print "Got SIGTERM\n";
$TERMINATE = true;
break;
case SIGKILL:
print "Got SIGKILL\n";
$TERMINATE = true;
break;
case SIGINT:
print "User pressed Ctrl+C - Got SIGINT\n";
$TERMINATE = true;
break;
}
}
while(true)
{
// Do everything and anything - though infinite loops, not the best idea.
if( $TERMINATE )
{
// Perform all cleaning functions.
// Then break the loop
break 2;
}
}
// Just because Ctl+C was passed it only broke the loop, you can add other hooks into your program to help capture this break and more quickly terminate your script.
?>