www.webdeveloper.com
Results 1 to 2 of 2

Thread: Problem with a cron script

  1. #1
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700

    Problem with a cron script

    Hi All,

    This has me utterly mystified. I have a cron that runs a php script. It used to work... now it doesn't, and nothing's been done to it. There are two parts to this problem... the cron and the script.

    Here's the cron...
    */10 * * * * root cd /usr/share/cms; /usr/bin/php5 /usr/share/cms/cron/cronsendmail.php > /var/log/cms/cronmail.log >/dev/null 2>&1

    The way I read that, every ten minutes, the Apache server,acting as root, runs a script called cronsendmail.php and any output ought to be written to cronmail.log.

    It certainly runs, because if I delete cronmail.log, ten minutes later, it's back. The problem is, it's an empty file.

    Therefore, I'm guessing the problem lies within the script. I'll post the entire script below... I've looked over it dozens of times and I can not see any error.

    PHP Code:
    //max emails per session
        
    $maxe 50;

    //set up the date
        
    $dte date("Y-m-d"time());
        
    $dx explode("-",$dte);
        
    $sqldate $dx[0]."-".$dx[1]."-".$dx[2];//yyyy-mm-dd

    //connect to master
         
    $mlink = @mysql_connect("localhost""user""password"TRUE) or die("Couldn't connect : ".mysql_error());
         @
    mysql_select_db('filename'$mlink) or die("Couldn't open filename : ".mysql_error());
         
    // I've sanitised user, password and filename, or course... they are correct in the running version.
        
        
    $qrl "INSERT INTO filename.syslog (type) VALUES ('mailcronst')";
        
    $rsl mysql_query($qrl);

    //get the mailer function
         
    require_once($fpath.'/includes/mail-smtp.inc.php');

    //get next 50 unsent mail
        
    $qrm "SELECT * FROM filename.mailqueue WHERE MQsent = 'N' ORDER BY MQdate ASC LIMIT ".$maxe;
        
    $rsm mysql_query($qrm$mlink);
        while(
    $rwm mysql_fetch_array($rsm))
        {
            
    $m++;
            
    $rn $rwm['MQrn'];
            
    $xto $rwm['MQto'];
            
    $xtoname $rwm['MQtoname'];
            
    $xfrom $rwm['MQfrom'];
            
    $xfromname $rwm['MQfromname'];
            
    $xsubject $rwm['MQsubject'];
            
    $xitem $rwm['MQmailitem'];
            
    $xmaildomain $rwm['MQdomain'];
            
    $xpassword $rwm['MQpassword'];
            
    $xtype $rwm['MQtype'];

            
    $sm explode("@",$xfrom);
            
    $smtp "mail.".$sm[1];

            
    $xheaders "Subject: " $xsubject "\r\n";
            
    $xheaders .= "To: ".$xto."\r\n";
            if(
    $xfromname==""){$xheaders .= "From: ".$xfrom."\r\n";}else{$xheaders .= "From: ".$xfromname." <" $xfrom ">\r\n";}
            
    $xheaders .= "Return-Path: ".$xfrom."\r\n";
            
    $xheaders .= "MIME-Version: 1.0\r\n";
            
    $xheaders .= "Content-Type: ".$xtype."; charset=ISO-8859-1\r\n";

            if(
    $xpassword!="")
            {
                print 
    "SENDING VIA SMTP<br/>";
                if(
    smtp($xto$xsubject$xitem$xfrom$smtp$xfrom$xpassword$xheaders));
                {
                    
    $qrl "INSERT INTO filename.maillog (MLdate, MLdomain, MLto, MLsubject) VALUES ('$sqldate', '$xmaildomain', '$xto', '$xsubject')";
                    
    $rsl mysql_query($qrl$mlink);
                
    //delete the mail
                    
    $qrd "DELETE FROM filename.mailqueue WHERE MQrn = '$rn'";
                    
    $rsd mysql_query($qrd$mlink);
                }
                else    if(
    mail($xto$xsubject$xitem$xheaders));
                {
                    
    $qrl "INSERT INTO filename.maillog (MLdate, MLdomain, MLto, MLsubject) VALUES ('$sqldate', '$xmaildomain', '$xto', '$xsubject')";
                    
    $rsl mysql_query($qrl$mlink);
                
    //delete the mail
                    
    $qrd "DELETE FROM filename.mailqueue WHERE MQrn = '$rn'";
                    
    $rsd mysql_query($qrd$mlink);
                }
            }
            else
            {
                print 
    "SENDING VIA MAIL<br/>";
                if(
    mail($xto$xsubject$xitem$xheaders));
                {
                    
    $qrl "INSERT INTO filename.maillog (MLdate, MLdomain, MLto, MLsubject) VALUES ('$sqldate', '$xmaildomain', '$xto', '$xsubject')";
                    
    $rsl mysql_query($qrl$mlink);
                
    //delete the mail
                    
    $qrd "DELETE FROM filename.mailqueue WHERE MQrn = '$rn'";
                    
    $rsd mysql_query($qrd$mlink);
                }
            }
        }
        
        
    $qrl "INSERT INTO filename.syslog (type) VALUES ('mailcronfn')";
        
    $rsl mysql_query($qrl);
        
        print 
    "Done!"
    The general idea is that when the CMS sends an email, it stores it in a file for sending in batches. I can then send via smtp rather than php's mail() function.

    This cron is supposed to connect to the database where the batched mails are filed, check the file, send the email, then delete them from the file after recording the outgoing mail in a maillog file.

    It then writes a simple record in syslog to tell me it's done.

    To recap... when the cron runs, I get a file opened as var/log/cms/cronmail.log. That file is empty, and nothing has been touched in the database.

    I thought it might be something to do with the error settings in my php.ini file, so I added error_reporting(E_ALL); to the top of the script... no difference.

    I then thought I'd just try commenting out everything except print "Done!" at the end... no difference... still a blank file.

    Finally, I tried it without the 'filename.' in the sql... no difference.

    This is driving me nuts and I would be most appreciative if someone might be able to point me in the right direction.

    Thanks
    CTB

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    What happens if you comment out the second redirection in the cron job?
    Code:
    */10 * * * * root cd /usr/share/cms; /usr/bin/php5 /usr/share/cms/cron/cronsendmail.php > /var/log/cms/cronmail.log #>/dev/null 2>&1
    (Hoping you'll at least see some debug info in the .log file.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles