/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=php]
//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 . “rn”;
$xheaders .= “To: “.$xto.”rn”;
if($xfromname==””){$xheaders .= “From: “.$xfrom.”rn”;}else{$xheaders .= “From: “.$xfromname.” <” . $xfrom . “>rn”;}
$xheaders .= “Return-Path: “.$xfrom.”rn”;
$xheaders .= “MIME-Version: 1.0rn”;
$xheaders .= “Content-Type: “.$xtype.”; charset=ISO-8859-1rn”;

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!”;
[/code]

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

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 18.2013 — What happens if you comment out the second redirection in the cron job?
<i>
</i>*/10 * * * * root cd /usr/share/cms; /usr/bin/php5 /usr/share/cms/cron/cronsendmail.php &gt; /var/log/cms/cronmail.log [COLOR="#FF0000"]#&gt;/dev/null 2&gt;&amp;1[/COLOR]


(Hoping you'll at least see some debug info in the .log file.)
×

Success!

Help @chestertb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...