Question

Im trying to setup a cron job which runs a php script every 24 hours that recreates a javascript file on the server. I have tried the following commands for cronjobs without any luck.

Note if I open/run this file from a browser it totally works!

both files have 777 file permissions, using CentOS release 5.8 and PHP 5.4.22 (cli)

PHP FILE AND JS ARE IN THE SAME FOLDER */home/pmos/public_html/cash/js/createjs.php /home/pmos/public_html/cash/js/custom.js*

Thanks

Commands I've tried

/usr/bin/php -f /home/pmos/public_html/cash/js/createjs.php
/usr/bin/wget --output-document=/dev/null https://www.website.com/cash/js/createjs.php
curl https://www.website.com/cash/js/createjs.php

the PHP script

<?php
$date = date("Y-m-d");
$date1 = str_replace('-', '/', $date);
$nextd = date('Y-m-d',strtotime($date1 . "+2 days"));
$date = DateTime::createFromFormat("Y-m-d", $nextd);
$year = $date->format("Y");
$month = ($date->format("m") -1);
$day = $date->format("d"); 

$myFile = "custom.js";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "jQuery(document).ready(function ($){ jQuery('a.scrollTo').click(function(){ jQuery.scrollTo( $(this).attr(\"href\"), { duration: 1000, easing:'easeInOutExpo', offset: -100 }); return false; }); if(jQuery.fn.countdown){ jQuery('span#countdown').countdown({ until: new Date(".
$year.", ".$month.", ".$day.
"), format: 'HMS' }); } });";
fwrite($fh, $stringData);
fclose($fh);
?>
Was it helpful?

Solution

I'm guessing your working directory isn't what you think it is.

$myFile = "custom.js";

could be anywhere.

https://unix.stackexchange.com/questions/38951/what-is-the-working-directory-when-cron-executes-a-job

(but that's only a guess since you actually haven't given us enough information to know)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top