Jump to content

PHP script running on a cron job


atl_andy

Recommended Posts

I have a script running at 8am on a cron job.  It uses Swift mailer to email a pdf document.  If I run the script from the command line everything works great.  But there are two issues when it is run using cron.  The first issue is the file sent will not update to the current date, it just sends the previous day's pdf.  The second issue was an attempt to get around the first.  I used unlink to delete the file and touch to create it again, thinking that would eliminate the repeating file problem. 

 

To summarize, I need the script to get data from a mysql table that will be emailed in a pdf every morning via a cron job.

 

The server is running CentOS 5.3 with LAMP.

 

Here's the select statement:

$result=mysql_query("SELECT src,clid,dcontext,calldate FROM cdr WHERE 
dcontext='from-internal' AND calldate>=DATE_ADD(CURDATE(),INTERVAL -21
HOUR) AND calldate<=DATE_ADD(CURDATE(),INTERVAL +3 HOUR) ORDER BY 
src");

 

Here's the cron job:

0 8 * * * 1,2,3,4,5,6 /usr/bin/php /var/www/reports/report.php

 

Here's the mail portion on the script:

// yesterday date
$yesterday=date('m/d/Y',mktime(0,0,0,date("m"),date("d")-1,date("Y")));

$pdf->Output("daily_report.pdf",'F');

// Create mail message
$message = Swift_Message::newInstance()
  // Subject
  ->setSubject("Daily Call Report - " . $yesterday)
  // From address
  ->setFrom(array('agriggs@impulsetech.us'=>'Andy Griggs'))
  // To address
  ->setTo(array('agriggs@impulsetech.us'=>'Andy Griggs'))
  // Body
  ->setBody('Daily Call Report For ' . $yesterday)
  // Add attachment
  -> 
attach(Swift_Attachment::fromPath('/var/www/reports/test.php'));

// Create the transport
$transport = Swift_MailTransport::newInstance();

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Send the message
$numSent = $mailer->send($message);

// Delete the daily report file after emailing it
unlink('daily_report.pdf');

// Create file so the cron job will work
touch("/var/www/reports/daily_report.pdf");

 

Any help is greatly appreciated!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.