Crontab 无法在 AWS ec2 Ubuntu 实例上运行


Crontab failing to run on AWS ec2 Ubuntu Instance

尝试设置一个 crontab 以在我的 AWS ec2 ubuntu 实例上运行 php 脚本:

* * * * * /usr/bin/php /var/www/html/tides/get_tides.php?id=PASSWORD

无法让它执行。

当我在 Web 浏览器中运行时,脚本会执行:

http://URL_NAME/tides/get_tides.php?id=PASSWORD

当我在命令行上运行"哪个 php"时,它会返回/usr/bin/php目录/文件权限(潮汐和get_tides.php):755

如果我在命令行上运行sudo /usr/bin/php /var/www/html/tides/get_tides.php?id=PASSWORD,我会得到: 无法打开输入文件:

/var/www/html/tides/get_tides.php?id=PASSWORD

有什么建议吗?

没有

直接的方法用 ?id=PASSWORD 运行 php scriping,可以调整 cronjob 运行为

* * * * * /usr/bin/curl -o temp.txt http://URL_NAME/tides/get_tides.php?id=PASSWORD

* * * * * /usr/bin/wget -q -O temp.txt http://URL_NAME/tides/get_tides.php?id=PASSWORD

由于您没有在get_tides.php中显示内容,因此也许您可以将命令行参数传递给PHP脚本

php /var/www/html/tides/get_tides.php PASSWORD

并将 PHP 更新为 :

<?php
// $argv[0] is '/var/www/html/tides/get_tides.php'
$id = $argv[1];
?>

问题是由我的 cron 脚本中未使用绝对路径引起的:

<?php
$path = $_SERVER["DOCUMENT_ROOT"];
$file = "$path/cron-test.txt";

我用/var/www/html替换了$_SERVER["DOCUMENT_ROOT"]

crantab 命令是:* * * * * /usr/bin/php /var/www/html/cron-test.php