0

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);
?>
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Do the "commands I've tried" actually work? and what have you tried putting in your crontab file? If you can get something running commandline, then the problem is with your cron call. Do you want your webserver running? or ... ? – DrLivingston Mar 29 '14 at 00:45
  • What happens when you try those things? – Michael Lorton Mar 29 '14 at 00:46
  • if I run the file directly from the browser, the JS is created, however when I setup a cron job nothing happens, I have also tried running it from command line and nothing happens – Daniel Adarve Mar 29 '14 at 01:14
  • Off topic question: is your webserver static? Why not just serve the js file as PHP? (when you request it put some URL parameters on it so the browser won't cache it). OR just put that date logic in the javascript itself so you don't have to do any of this? – DrLivingston Mar 29 '14 at 01:19
  • cant use the logic inside the javascript itself because the timer would only work when someone views the page, so this timer would reset everytime someone loads the page – Daniel Adarve Mar 29 '14 at 01:24

1 Answers1

0

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)

Community
  • 1
  • 1
DrLivingston
  • 788
  • 6
  • 15
  • the JS file is in the same directory as the php file – Daniel Adarve Mar 29 '14 at 01:12
  • That doesn't mean thats going to be your `current working directory` when the cron job is executed. I'm assuming you have a random custom.js file being written somewhere - like you home directory. – DrLivingston Mar 29 '14 at 01:14
  • I would suggest changing the value of $myFile to have full path information. – DrLivingston Mar 29 '14 at 01:15
  • 1
    so it would be something like $myFile = "/home/pmos/public_html/cash/js/custom.js"; – Daniel Adarve Mar 29 '14 at 01:19
  • yes if that's where you're serving it from. chron isn't going to know to cd to /home/pmos/public_html/cash/js/ before it creates your file. So you either need to tell it exactly where you want the file, or change directories before you execute your chron job. – DrLivingston Mar 29 '14 at 01:21
  • Glad you got it working. (Although now read the comments above about whether or not you really need to go through all this trouble in the first place.) – DrLivingston Mar 29 '14 at 01:37