I am working on site .suddenly i got this message on running cron job multiple times.i check my website for the same error but it works.I remove the cache and try to exit the code but after the error no code works in cron job.This is my code perhaps someone can guide me where the issue is :


class Namespace_Module_Model_Observer 

   public function importemails(Varien_Event_Observer $observer)
        echo "Hi Dear";exit();

      /* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'mypass';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {

  /* begin output var */
  $output = '';

  /* put the newest emails on top */

  /* for every email... */
  foreach($emails as $email_number) {

    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $message = imap_fetchbody($inbox,$email_number,2);

    /* output the email header information */
    $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
    $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
    $output.= '<span class="from">'.$overview[0]->from.'</span>';
    $output.= '<span class="date">on '.$overview[0]->date.'</span>';
    $output.= '</div>';

    /* output the email body */
    $output.= '<div class="body">'.$message.'</div>';

  echo $output;

/* close the connection */



this code works for several hours but all of sudden it showing me the error of mysql gone away.Thanks in advance

  • This does not look like an issue related to Magento in special. most probably your MySQL server crashes due to a large number of queries (or maybe something else). This is more of a sys admin question.
    – Marius
    Aug 28, 2013 at 11:06
  • @Marius Thanks for your replay.I am confused because the same database running for the site is working well , but cron job code produce this error. Aug 28, 2013 at 11:10

1 Answer 1


Based on the code I see above, the connection to the database is sitting idle while you iterate over all the emails. My guess is that the code here is taking longer than MySql is configured to wait before closing an idle connection, I.e. The value of the wait_timeout setting which IIRC is 300 (5 min) by default.

Without seeing the exact backtrack of the error, I would assume it's coming after this finishes processing, when the cron dispatcher attempts to update the cron task as complete in the cron_schedule table.

Out of the box, Magento does not attempt to reopen connections MySql has closed and will fail if it's been closed and it tries to use it.

To fix this, you'll either need to up the timeout setting in the mysql stack or ping the database to maintain an open connection.

  • The other common cause for error 2006 is a max_allowed_packet to small for necessary queries, or something poorly designed in custom code which is building something to large to fit in a packet. Since you aren't touching mysql, I am hesitant that this could be related. If it's not one of these two reasons, it's going to be complicated, as @marius pointed out in his comments above.
    – davidalger
    Aug 29, 2013 at 2:02
  • After one day while coming back to code its working now.I just try to read some data from database .currently i am using resource class and model.like $resource = Mage::getSingleton('core/resource'). what i have to do to avoid the problem again ? Aug 29, 2013 at 4:27
  • My cron job runs after every one minute. Aug 29, 2013 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.