cron 任务 - 每 24 小时使用 PHP 创建 JS 文件


cron task - Create JS file using PHP every 24 hours

我试图设置一个cron作业,该作业每24小时运行一次php脚本,在服务器上重新创建一个javascript文件。我已经为cronjobs尝试了以下命令,但没有任何运气。

请注意,如果我从浏览器打开/运行此文件,它完全有效!

这两个文件都有 777 个文件权限,使用 CentOS 版本 5.8 和 PHP 5.4.22 (cli)

PHP 文件和 JS 位于同一文件夹中*/home/pmos/public_html/cash/js/createjs.php/home/pmos/public_html/cash/js/custom.js*

谢谢

我尝试过的命令

/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

PHP 脚本

<?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);
?>

我猜你的工作目录不是你想象的那样。

$myFile = "custom.js";

可以在任何地方。

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

(但这只是一个猜测,因为你实际上没有给我们足够的信息来知道)