Magento – Cron job hang – how to debug

cronmagento-1.9

I have some cronjob that sometimes gets stuck in running status.

That makes me think that for some reason they produce an error but I cannot find any related log. ( the column messages in cron_schedule is empty )

How can I be sure cron execution produces logs in case of errors?
How can I proceed to debug this issue, any advice?

UPDATE
It looks like my question is not clear, so I'm adding some more content ( I am not sure it gonna help because most of the people seem to read the title and guess the question … )

  • I know how cron works
  • I know how to check if a cronjobs run or not
  • My cronjobs correctly run

The problem is some of the cronjobs do not end.

To be more clear: Mage_Cron_Model_Observer::_processJob()

  $schedule
        ->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', time()))
        ->save();

    call_user_func_array($callback, $arguments);

    $schedule
        ->setStatus(Mage_Cron_Model_Schedule::STATUS_SUCCESS)
        ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time()));

The status is never reached for some cron, that is the problem.

Please avoid give the random answer or just the first result google provide ( I know how to use google, I did my research … )

If the answer is not clear, just let me know.
If you wanna help you are more than welcome if you just won't waste my and your time you are not. ( this site is meant for quality answers, it is not a forum where everybody says his opinion … posting not related answers will not help other people with the same issue … but will just create confusion )

Best Answer

All of the fields should speak for themselves.

Copy and save the following PHP script.

<?php

    // Parse magento's local.xml to get db info, if local.xml is found

    if (file_exists('app/etc/local.xml')) {

    $xml = simplexml_load_file('app/etc/local.xml');

    $tblprefix = $xml->global->resources->db->table_prefix;
    $dbhost = $xml->global->resources->default_setup->connection->host;
    $dbuser = $xml->global->resources->default_setup->connection->username;
    $dbpass = $xml->global->resources->default_setup->connection->password;
    $dbname = $xml->global->resources->default_setup->connection->dbname;

    }

    else {
        exit('Failed to open app/etc/local.xml');
    }

    // DB Interaction
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to <a class="HelpLink" onclick="showHelpTip(event, hint_id_7); return false;" href="javascript:void(0)">mysql</a>');
    mysql_select_db($dbname);

    $result = mysql_query("SELECT * FROM " . $tblprefix . "cron_schedule") or die (mysql_error());


    // CSS for NexStyle
    echo '
    <html>
    <head>
    <title=Magento Cron <span style='background-color:#CCFF00;'>Status</span>>
    <style type="text/css">
    html {
        width: 100%;
        font-family: Helvetica, Arial, sans-serif;
    }
    body {
        background-color:#00AEEF;
        color:#FFFFFF;
        line-height:1.0em;
        font-size: 125%;
    }
    b {
        color: #FFFFFF;
    }
    table{
        border-spacing: 1px;
        border-collapse: collapse;
        width: 300px;
    }
    th {
        text-align: center;
        font-size: 125%;
        font-weight: bold;
        padding: 5px;
        border: 2px solid #FFFFFF;
        background: #00AEEF;
        color: #FFFFFF;
    }
    td {
        text-align: left;
        padding: 4px;
        border: 2px solid #FFFFFF;
        color: #FFFFFF;
        background: #666;
    }
    </style>
    </head>';

    // DB info for user to see
    echo '
    <body>
    <a href="http://nexcess.net">
    <img src="https://smhttp-nex.nexcesscdn.net/803313/static/images/logoMainR2.gif" width="217" height="38" alt="Nexcess Beyond Hosting"></a>



    <b>Table Prefix:</b> ' . $tblprefix . ''
    . '<b>DB Host:</b> ' . $dbhost . ''
    . '<b>DB User:</b> ' . $dbuser . ''
    . '<b>DB Name</b>: ' . $dbname . '</p>';

    // Set up <span style="background-color:#CCFF00;">the</span> table
    echo "
            <table border='1'>
            <thread>
            <tr>
            <th>schedule_id</th>
               <th>job_code</th>
               <th><span style="background-color:#CCFF00;">status</span></th>
               <th>messages</th>
               <th>created_at</th>
               <th>scheduled_at</th>
               <th>executed_at</th>
               <th>finished_at</th>
               </tr>
               </thread>
               <tbody>";

    // Display <span style="background-color:#CCFF00;">the</span> data from <span style="background-color:#CCFF00;">the</span> query
    while ($row = mysql_fetch_array($result)) {
               echo "<tr>";
               echo "<td>" . $row['schedule_id'] . "</td>";
               echo "<td>" . $row['job_code'] . "</td>";
               echo "<td>" . $row['<span style="background-color:#CCFF00;">status</span>'] . "</td>";
               echo "<td>" . $row['messages'] . "</td>";
               echo "<td>" . $row['created_at'] . "</td>";
               echo "<td>" . $row['scheduled_at'] . "</td>";
               echo "<td>" . $row['executed_at'] . "</td>";
               echo "<td>" . $row['finished_at'] . "</td>";
               echo "</tr>";
    }

    // Close table and last few tags
    echo "</tbody></table></body></html>";

    mysql_close($conn);
    ?>
Related Topic