执行在 Cron 上运行的 PHP 脚本时出现问题

发布于 2024-12-06 03:46:16 字数 395 浏览 0 评论 0原文

我试图在第三方托管服务器上的 cron 上运行一个非常简单的 PHP 脚本,但遇到了一个奇怪的问题。尽管我确认 cron 确实在计划的时间内执行了脚本,但脚本的内容并未执行。我无法检查 Cron 设置,因为它不在我的服务器上,但我想知道我是否做错了什么?

该脚本仅用于测试目的,因此非常简单:

<?php
$file = fopen("file-" . rand(1000, 9999) . '.txt', 'w');
fwrite($file, 'cron');
fclose($file);
?>

当我手动运行此脚本时,将创建该文件。当我把它留在 Cron 上时,什么也没有发生。可能是什么原因?脚本的权限是777。是我做错了什么还是服务器有问题?

非常感谢。

I'm trying to run a very simple PHP script on cron on a third-party hosting server and I have encountered a weird problem. The script's contents is NOT executed although I got confirmed that the cron does execute the script in scheduled times. I cannot check the Cron settings as it is not on my server, yet I would like to know if there is something I do wrong?

The script is for testing purpose only and thus it is very simple:

<?php
$file = fopen("file-" . rand(1000, 9999) . '.txt', 'w');
fwrite($file, 'cron');
fclose($file);
?>

When I run this script manually, the file is created. When I leave it on Cron, nothing happens. What can be the cause? The permissions of the script are 777. Do I do something wrong or is the problem on a server?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2024-12-13 03:46:16

请记住,在 cron 下运行的脚本与在命令行上手动运行的相同脚本具有非常不同的环境。特别是,cron 将具有与 shell 提示符不同的工作目录。您没有为文件指定绝对路径,因此当 cron 运行脚本时,它将尝试在当时的 CWD 目录中创建该文件。如果运行此作业的用户 ID 对该目录没有写权限(或者文件存在但由其他人拥有),则 fopen 将失败。

切勿假设文件操作成功。始终检查错误情况。当您使用它时,指定文件的绝对路径,这样您就知道它将被写入哪里:

$filename = "/path/to/where/you/wantfile-" . rand(1000,9999) . '.txt';
$file = fopen($filename, 'w') or die("Unable to open $filename for output");

Remember that a script running under cron has a very different environment than the same script being run manually on the command line. In particular, cron will have a different working directory than the shell prompt. You don't specify an absolute path for your file, so when the script is run by cron, it'll try to create that file in whatever directory is the CWD at that time. If the userid this job runs under doesn't have write permissions on that directory (or the file exists but owned by someone else), then the fopen will fail.

Never assume that a file operation succeeds. Always check for error conditions. And while you're at it, specify an absolute path for the file, so you KNOW where it will be written:

$filename = "/path/to/where/you/wantfile-" . rand(1000,9999) . '.txt';
$file = fopen($filename, 'w') or die("Unable to open $filename for output");
踏雪无痕 2024-12-13 03:46:16

这可能是因为该脚本是以 apache 以外的其他用户身份运行的。在某些情况下,我会使用curl并让脚本检查IP是否是本地IP,以确保它是从正确的机器运行的。当然,有更少的黑客方法可以实现这一目标,但这始终是一种快速而简单的方法。

This probably because the script is run as some user other than apache. In some cases I will use curl and make the script check to see if the IP is the local IP to make sure it is being run from the right machine. There certainly less hackish ways to accomplish this, but this is always a quick and easy one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文