Cron Job -- PHP script not running correctly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
civil777
Forum Newbie
Posts: 3
Joined: Fri Oct 30, 2009 7:19 am

Cron Job -- PHP script not running correctly

Post by civil777 »

I am trying to run a php script using Cron, and I am having a problem getting Cron to create a new file. (For those of you not familiar, Cron is a a system program that can execute a script at a specific time).

Here's what I have working:
I got Cron to run a script that will read and write my MySQL database.

Here's what is not working: Now I am trying to get Cron to run a script that will generate an Excel file and save it to the server. I am using a PEAR Excel script to do the generation. I am able to generate the Excel file if I run the script in my browser, BUT (and here's my problem) the script will not generate the Excel file if it is run from Cron. I can't figure out why. Here's the script:

File generate_excel_file.php:

Code: Select all

 
 
#!/usr/bin/php5
<?php
require_once "Spreadsheet/Excel/Writer.php";
// Create an instance
$xls = &new Spreadsheet_Excel_Writer();
// Send HTTP headers to tell the browser what's coming
$xls->send("test.xls");
// Add a worksheet to the file, returning an object to add data to
$sheet = &$xls->addWorksheet('Binary Count');
// Write some numbers
for ($i = 0;$i < 11;$i++) {
  // Use PHP's decbin() function to convert integer to binary
  $sheet->write($i, 'test', decbin($i));
}
// Finish the spreadsheet, dumping it to the browser
$xls->close();
?> 
 
 
(Permissions on this file are set to 777.)

Here's the crontab file:

Code: Select all

 
 
48 2 15 * * /var/www/generate_excel_file.php
 
 

Anyone know what might be wrong? Thanks for the help!!!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cron Job -- PHP script not running correctly

Post by AbraCadaver »

Well, your script is sending the excel file as data to the browser. If you run it as a cron, where is the data supposed to be sent? I assume that the Spreadsheet/Excel/Writer.php class has a way to save the file to the file system.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
UdayKarkera
Forum Newbie
Posts: 1
Joined: Thu Feb 12, 2015 7:45 am

Re: Cron Job -- PHP script not running correctly

Post by UdayKarkera »

I have the same question and i also agree with you @AbraCadaver but what is the solution for this. I want to generate Excel Sheets at a particular time daily. What should i do? Even I have the code in PHP
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Cron Job -- PHP script not running correctly

Post by Celauran »

As was mentioned initially, have your writer write to a file instead of sending it to the browser. How specifically you would accomplish this would depend on the library you're using.
Post Reply