4

I'm running php scripts from CLI, and i'd like to execute a function when script stopped with ctrl + c. I tried this:

<?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);

but it doesn't works. How can i do this?

Lay András
  • 795
  • 2
  • 9
  • 14

1 Answers1

1

When attaching the handler in pcntl_signal, use SIGINT instead of SIGTERM

Nadir
  • 1,799
  • 12
  • 20
  • 3
    It might be safer to attach handlers for both signals. [Related question about which signals are fatal](http://stackoverflow.com/questions/13219071/which-fatal-signals-should-a-user-level-program-catch) – amphetamachine Feb 18 '16 at 14:56
  • 3
    Well, depends on what OP wants to achieve. If he only wants to handle Ctrl-c, it is enough. – Nadir Feb 18 '16 at 14:57