Skip to main content

php cronjob interruption



i have a cronjob in php that calculates few business rules (eg: net rev, gross rev, estimated rev, etc... using standard deviation & other math algo)





this cronjob calls multiple cron calls 3 php scripts using exec





for each calls i start a process in background and tell the system that jobs x started. eg:





here's my logic







start

main cron start - message

cron sub 1 start - message

run cron sub 1

cron sub 1 end - message



wait until cron sub 1 stop



process stuff



cron sub 2 start - message

run background cron sub 2 // this will automaticaly send a message when this jobs end



cron sub 3 start - message

run background cron sub 3 // this will automaticaly send a message when this jobs end



main cron end - message



end







what i need to fix is this





if someone runs the job manually and cancel it (ctrl+c) or something bad happen in the cron (fatal error or cannot connect to db) or anything else (like not enough memory)





i want to stop the cronjob and tell what happen and when it stopped so i can start back where it was.





is it possible? thanks


Comments

  1. Here's a simple code you have to try/debug:

    <?php
    ignore_user_abort(false);

    function shutdown() {
    echo "shutdown function\n";
    }
    register_shutdown_function('shutdown');

    class shutdown {
    function __construct() {
    echo "shutdown::construct\n";
    }
    function __destruct() {
    echo "shutdown::destruct\n";
    }
    }

    $s = new shutdown();

    declare(ticks = 1);

    function sig_handler($signo) {

    switch ($signo) {
    case SIGTERM:
    echo "SIG: handle shutdown tasks\n";
    break;
    case SIGHUP:
    echo "SIG: handle restart tasks\n";
    break;
    case SIGUSR1:
    echo "SIG: Caught SIGUSR1\n";
    break;
    case SIGINT:
    echo "SIG: Caught CTRL+C\n";
    break;
    default:
    echo "SIG: handle all other signals\n";
    break;
    }
    return;
    }

    echo "Installing signal handler...\n";

    pcntl_signal(SIGTERM, "sig_handler");
    pcntl_signal(SIGHUP, "sig_handler");
    pcntl_signal(SIGUSR1, "sig_handler");
    pcntl_signal(SIGINT, "sig_handler");

    echo "Generating signal SIGTERM to self...\n";

    # killing the script using bad memory allocation
    /*
    ini_set('memory_limit', '1K');
    $data = '';
    for($i = 0; $i < 1000; $i++) {
    $data .= str_repeat($i . time(), time());
    }
    */

    # simulating CTRL+C
    // for($i = 0; $i < 100000000; $i++) { } // don't forget to press CTRL+C

    echo "Done\n";


    if the cronjob runs normally you will get:

    shutdown::construct
    Installing signal handler...
    Generating signal SIGTERM to self...
    Done
    shutdown function
    shutdown::destruct


    if you get a Fatal error:

    shutdown::construct
    Installing signal handler...
    Generating signal SIGTERM to self...
    PHP Fatal error: Possible integer overflow in memory allocation (11 * 1321404273 + 1) in /var/www/domain.com/php.php on line 51
    shutdown function


    if you kill the process:

    kill <processid>

    shutdown::construct
    Installing signal handler...
    Generating signal SIGTERM to self...
    SIG: handle shutdown tasks
    Done
    shutdown function
    shutdown::destruct


    you can use posix_kill(posix_getpid(), <signal you like>); in each cases also.

    read more:

    http://php.net/manual/en/function.pcntl-signal.php

    http://en.wikipedia.org/wiki/Signal_%28computing%29

    http://users.actcom.co.il/~choo/lupg/tutorials/signals/signals-programming.html

    ReplyDelete
  2. If this is a bash script, you can trap the ctrl+c signal like so:

    is_borked()
    {
    echo "Somebody set up us the bomb"
    exit
    }

    trap is_borked SIGINT


    It'd be up to you to keep track (via variable?) at which step this happened, if you need some kind of a cleanup.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?