0

I am setting up a CRON to detect a change on a page. The issue I am having is pointing more to server side but I would like some others to look at the code and see if any obvious problems stick out that would hinder the CRON from running right. The CRON runs the script and works some of the time on its own, the results are either 100% correct or the email is delayed with the wrong time stamp coming from $nws_timestamp in the message.

When the script is run via the browser is works every time correctly.

Summery of the script; reads a text file with the remote data saved on it and compares the two, pulls and trims just the date and time of update. If different it sends an email to a few emails with the updated date and time.

Purpose of script: Allows a list of people to be notified when the National Weather Service updates the weather briefings during storm events. (like the current tropical storm on us now)

    <?
$source = file_get_contents('http://www.erh.noaa.gov/mhx/downloads/briefings/index.php');
$textfile = 'nws_brief.txt';

list($junk,$nws_timestamp) = explode("Updated: </td><td align=\"left\">",$source);

$nws_timestamp = substr("$nws_timestamp", 0, 26);                       //location of text on page
$nws_timestamp = str_replace("</td>", "", "$nws_timestamp");
$nws_timestamp = str_replace("</tr>", "", "$nws_timestamp");
$nws_timestamp = str_replace("<", "", "$nws_timestamp");
$nws_timestamp = str_replace("/", "", "$nws_timestamp");
$nws_timestamp = str_replace("/t", "", "$nws_timestamp");
$nws_timestamp = str_replace("td", "", "$nws_timestamp");
$nws_timestamp = str_replace("d>", "", "$nws_timestamp");
$nws_timestamp = str_replace(">", "", "$nws_timestamp");

$textfile_data = file_get_contents($textfile);

//READ FROM FILE
if ($textfile_data == $nws_timestamp){
        exit;
    }else{
        //Continue
    }

//SAVE TO FILE
$current = file_get_contents($textfile);
$current = "$nws_timestamp";
file_put_contents($textfile, $current);

//EMAIL NOTIFICATION

//BCC List:
$bcc_list = array(
  "user2@domain.com",
  "user3@domain.com",
  "user4@domain.com",
  "user5@domain.com",


);
$bcc = implode(',', $bcc_list);                          


putenv('TZ=America/New_York');                          
$date_time = date('m-d-Y g:i:s A');                     

$to = "user1@domain.com";                           
$headers = "From: OBXAirWaves <admin@obxairwaves.com>\r\n"; 
$headers .= "Organization: OBXAirWaves.com\r\n"; 
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "BCC: $bcc\r\n";

$subject = "NWS Briefing Update - $nws_timestamp";                      
$msg = "NWS Update  - $date_time<br>
<a href=\"http://www.erh.noaa.gov/mhx/downloads/briefings/index.php  \">Download Latest Briefing</a><br>
<br>
Briefing Update:<br>
<table border='1'>
  <tr>
    <td>New: $nws_timestamp</td>
  </tr>
  <tr>
    <td>Last: $textfile_data</td>
  </tr>
</table>


";

mail($to, $subject, $msg, $headers);

echo "Date & Time: $date_time<br>To: $to<br>BCC: $bcc<br>Subject: $subject<br><br>Message: <br>$msg";

?>
Anthony
  • 79
  • 1
  • 4
  • Also here is the CRON: 12,32,52 * * * * php -q /home/mindbr5/public_html/site_accounts/~obxairwaves_com/cron/nws_brief.php – Anthony May 10 '15 at 19:10
  • What does your cron log say? – Machavity May 10 '15 at 19:13
  • http://stackoverflow.com/questions/13259530/using-cron-jobs-to-visit-url – SaidbakR May 10 '15 at 19:15
  • Machavity - I believe I do not have access to the log, var/log/cron, its a shared server. If there is a different way let me know. – Anthony May 10 '15 at 19:36
  • Support sent me a copy of the lines in the log. Looks like its running fine with out error. May 10 16:12:01 ecbiz66 crond[8198]: (mindbr5) CMD (php -q /home/mindbr5/public_html/site_accounts/~obxairwaves_com/cron/nws_brief.php) – Anthony May 10 '15 at 20:22
  • I have found that the cron is working fine, and the script is being run when called. The problem is the mails are getting jammed up somewhere. I am going to work on a different mail method tomorrow. Any suggestions for sending the mail differently or correctly would be appreciated. – Anthony May 10 '15 at 23:10

1 Answers1

0

Solved;

I ended up running a few more tests and found that the script was running fine, and there was not problem with the location of the script being run and calling files.

The problem resided in the mail() function being used in the cron, I have moved over to PHPMailer to send the mail in the script and logging in to SMTP to send the mail. Works every time on time.

Thanks for the help,

Anthony
  • 79
  • 1
  • 4