atl_andy Posted November 13, 2009 Share Posted November 13, 2009 Hello, I have a PHP script that will create a PDF then email it as an attachment. Everything works great if I run the script manually from the command line. The PDF is created for the date range of the SQL query, and the email is sent to the recipients listed in the script. However, if I try to run the script in a cron job, only the email portion of the script will work; and that part will work only if the previous day's PDF is in the directory. Nothing will work if there is no file from the previous day ( I have tried to use unlink() to delete the file to prevent the previous day's email from being sent...unsuccessfully). Also, if the job runs on Tuesday and Wednesday, the PDF for Tuesday will be send each time. I'm baffled on which part of the system is not working and what to troubleshoot. I read, one a post somewhere, that to get cron to work with PHP the --enable-cgi flag might need to be set during compilation. This is running on an Asterisk server so there are no page visits that will allow the script to be run when a page is visited, or to use another web based cron-style application. I also checked the file permission and made the script executable. Cron job (I checked the logs and it runs without errors): 0 8 * * 1,2,3,4,5,6 /usr/bin/php -q /var/www/reports/reports.php Any help or insight would be appreciated. I can post code from the script if necessary, but I don't think that is the problem. Quote Link to comment https://forums.phpfreaks.com/topic/181420-solved-php-and-cron/ Share on other sites More sharing options...
trq Posted November 13, 2009 Share Posted November 13, 2009 I can post code from the script if necessary, but I don't think that is the problem. Indeed it would be. Theres nothing wrong with your crontab entry. Quote Link to comment https://forums.phpfreaks.com/topic/181420-solved-php-and-cron/#findComment-957090 Share on other sites More sharing options...
steviewdr Posted November 16, 2009 Share Posted November 16, 2009 Where is the pdf created? Is it created in /tmp before been emailed? Check permissions to see if the pdf can be created. -steve Quote Link to comment https://forums.phpfreaks.com/topic/181420-solved-php-and-cron/#findComment-958336 Share on other sites More sharing options...
atl_andy Posted November 16, 2009 Author Share Posted November 16, 2009 The file is created and saved in /var/www. Required files, connection, and select query: #!/usr/bin/php -q -c <?php require('/var/www/html/admin/cdr/lib/fpdf.php'); require_once('/var/www/reports/Swift-4.0.5/lib/swift_required.php'); $conn=mysql_connect("localhost","user","pass"); mysql_select_db("asteriskcdrdb",$conn); if(!$conn) { echo "Cannot connect to db"; } // Select query $result=mysql_query("SELECT src,clid,dcontext,calldate FROM cdr WHERE dcontext='from-internal' AND calldate>=DATE_ADD(CURDATE(),INTERVAL -45 HOUR) AND calldate<=DATE_ADD(CURDATE(),INTERVAL -21 HOUR) ORDER BY src"); if(!$result) { echo "Cannot process query"; } PDF creation, save and email using Swift mailer class PDF extends FPDF { private $_yesterday1; // Page Header function Header() { // yesterday date $_yesterday1=date('m/d/Y',mktime(0,0,0,date("m"),date("d")-1,date("Y"))); // Arial bold 15 $this->SetFont('Arial','B',14); // Title $this->Cell(0,10,$_yesterday1,0,0,'C'); // Line Break $this->Ln(20); } } // Create a new PDF file $pdf = new PDF(); $pdf->AddPage(); // Field Name position $Y_Fields_Name_position = 20; // Table position, under Field Name position $Y_Table_Position = 26; // First create each field name // Gray color filling each Field Name box $pdf->SetFillColor(232,232,232); // Bold font for Field Name $pdf->SetFont('Arial','B',12); $pdf->SetY($Y_Fields_Name_position); $pdf->SetX(5); $pdf->Cell(45,6,'Sales Rep',1,0,'L',1); $pdf->SetX(50); $pdf->Cell(45,6,'Calls Made',1,0,'L',1); $pdf->Ln(); // Formulate call results while($row=mysql_fetch_object($result)) { if(preg_match("/Conor/",$row->clid) && preg_match("/2164/",$row->clid)) { $conor += 1; } if(preg_match("/Shon/",$row->clid) && preg_match("/7733/",$row->clid)) { $shon += 1; } // Show Conor; $pdf->SetFont('Arial','',12); $pdf->SetY($Y_Table_Position); $pdf->SetX(5); $pdf->MultiCell(45,6,"Conor",1); $pdf->SetY($Y_Table_Position); $pdf->SetX(50); $pdf->MultiCell(45,6,$conor,1); $pdf->SetY($Y_Table_Position); // Show Shon $pdf->SetFont('Arial','',12); $pdf->SetY($Y_Table_Position+6); $pdf->SetX(5); $pdf->MultiCell(45,6,"Shon",1); $pdf->SetY($Y_Table_Position+6); $pdf->SetX(50); $pdf->MultiCell(45,6,$shon,1); $pdf->SetY($Y_Table_Position+6); // 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('email@email.com'=>'User')) // To address ->setTo(array('email@email.com'=>'User')) // Body ->setBody('Daily Call Report For ' . $yesterday) // Add attachment -> attach(Swift_Attachment::fromPath('/var/www/reports/daily_report.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); //printf("Sent %d messages\n", $numSent); //if ($mailer->send($message)) //{ echo "Sent\n"; } //else //{ echo "Failed\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/181420-solved-php-and-cron/#findComment-958410 Share on other sites More sharing options...
atl_andy Posted November 20, 2009 Author Share Posted November 20, 2009 ok, I checked the permission on the path to the file and it was ok. As long as the file exists, it will be emailed. If the file doesn't exist, it won't be created. The issue has to be the PDF creation portion of the script. I'm stuck on how to proceed. Quote Link to comment https://forums.phpfreaks.com/topic/181420-solved-php-and-cron/#findComment-961983 Share on other sites More sharing options...
DavidAM Posted November 20, 2009 Share Posted November 20, 2009 It works when run manually from the command line ... it does not work when run from cron. This would indicate a difference in the shell environment when cron is running it vs. when logged in and running it. $pdf-Output() is not specifying an absolute pathname for the file to be created. Unless that method is prepending a path name, it is trying to create the file in the "current working directory". What is your current working directory when running a cron script? Typically, cron will mail any output (including error messages) from a cron job to the user. I don't know how this worksout on a hosted site. Are you getting any strange emails that might be indicating an error in the job? Try adding to the end of your cron command >>/var/www/reports/errors.txt 2>&1 (use a path that you have write access to) then after the cron job runs, take a look at errors.txt and see if you have any error messages there. Quote Link to comment https://forums.phpfreaks.com/topic/181420-solved-php-and-cron/#findComment-962090 Share on other sites More sharing options...
atl_andy Posted November 20, 2009 Author Share Posted November 20, 2009 That was the problem. I figured the output would be in the cwd...guess not. It worked after I put in the absolute path. Quote Link to comment https://forums.phpfreaks.com/topic/181420-solved-php-and-cron/#findComment-962102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.