I attempted to create my first serious task with cron that has to download remote file (currency rates) and save it locally. My setup is W7/WMware Workstation 12 Player + Ubuntu 16.04. Everything works perfectly as I run it on localhost or debug it (netbeans). But I get strange errors when cron job is run (every 2nd minute) -
*/2 * * * * /var/www/html/myproject/cron/cnb.php
my php file looks like this:
<?php
//ini_set('display_errors',1);
//error_reporting(E_ALL);
Define("RATES_FILE", "http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt");
$rates= file(RATES_FILE);
if($rates){
$myfile = fopen("/var/www/html/myproject/cron/rates.txt", "w");
$info = getdate();
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$hour:$min:$sec";
foreach ($rates as $v) {
fwrite($myfile, $v);
}
fwrite($myfile,"Downloaded: ".$current_date);
fclose($myfile);
}
exit();
?>
When run locally from web browser the rates.txt is created properly. But cron produces following errors (info sent via email) :
/var/www/html/myproject/cron/cnb.php: 1:
/var/www/html/myproject/cron/cnb.php: cannot open ?php: No such file
/var/www/html/myproject/cron/cnb.php: 2:
/var/www/html/myproject/cron/cnb.php: Syntax error: word unexpected (expecting ")")
Same result is given when I run simplier code:
<?php
//ini_set('display_errors',1);
//error_reporting(E_ALL);
$myfile = fopen("/var/www/html/myproject/cron/rates.txt", "w");
$info = getdate();
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];
$current_date = "$hour:$min:$sec";
fwrite($myfile,"Downloaded: ".$current_date);
fclose($myfile);
exit();
?>
Is there anything I overlooked or ignored in terms of cron practice ? Thank you for help.