13

Is it possible to invoke a function when a cron process is killed from the command line (via Ctrl+c) or with the kill command?

I have tried register_shutdown_function(), but it doesn't seem to be invoked when the script is killed, but does get invoked when the script ends normally.

I am trying to log the result to a file and update a database value when a cron instance is killed automatically (ie. has been running too long).

halfer
  • 19,824
  • 17
  • 99
  • 186
Wei
  • 135
  • 1
  • 5
  • What do you mean by "killed automatically": The system's time limit stopping the process, or PHP's stopping the script? – Pekka Oct 11 '10 at 20:44
  • i have another cron instance that kills crons that have been running in the background after x seconds. – Wei Oct 11 '10 at 20:46
  • Maybe it'd simpler then to log in the cron script that kills ... – giraff Oct 11 '10 at 20:48

1 Answers1

15

According to a comment in the manual on register_shutdown_function(), this can be done the following way:

When using CLI ( and perhaps command line without CLI - I didn't test it) the shutdown function doesn't get called if the process gets a SIGINT or SIGTERM. only the natural exit of PHP calls the shutdown function. To overcome the problem compile the command line interpreter with --enable-pcntl and add this code:

 <?php
 declare(ticks = 1); // enable signal handling
 function sigint()  { 
    exit;  
 }  
 pcntl_signal(SIGINT, 'sigint');  
 pcntl_signal(SIGTERM, 'sigint');  
 ?>

This way when the process recieves one of those signals, it quits normaly, and the shutdown function gets called. ... (abbreviating to save space, read the full text)

If that is too much hassle, I would consider doing the timing out from within PHP by setting a time limit instead. Reaching the limit will throw a fatal error, and the shutdown function should get called normally.

cmc
  • 4,294
  • 2
  • 35
  • 34
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • That's cool, but I don't think I'll be able to recompile php on a production machine. – Wei Oct 11 '10 at 20:57
  • @Wei Why not rely on PHP's time limit instead? (Provided it works on CLI - I have never tried, and some comments [here](http://de.php.net/manual/en/function.set-time-limit.php) suggest it is hard coded to 0 - you'd have to try out) – Pekka Oct 11 '10 at 21:01
  • @Wei also, `pcntl` may already be enabled in your PHP version, check it – Pekka Oct 11 '10 at 21:02
  • The time limit would only work on one of the kill parameters. I have other parameters in place as well including memory usage and cpu usage. – Wei Oct 11 '10 at 21:06
  • @Wei well, you explicitly mentioned the time limit only... No idea whether there is another way, at least not using `kill` – Pekka Oct 11 '10 at 21:45
  • 3
    note that as of PHP 4.3 (which everyone should be at least using), you need to start this answer example code with `declare(ticks = 1);` for `pcntl_signal` to work. docs: http://php.net/pcntl_signal – georgiecasey Apr 22 '15 at 21:54
  • @georgiecasey: I suspect in your above comment, you meant 5.3! – halfer Apr 03 '17 at 19:05