6

我有几个 PHP 文件要由 cron 运行。我使用命令设置 crons-

crontab crontab.txt

在 crontab.txt 文件中,我编写了这样的 cron 命令:-

#(Updating tutor activities) - every minute
* * * * * /usr/bin/wget -O - -q -t 1 http://project/cron/tutor_activities.php

但是所有功能都不起作用(数据库查询、发送提醒邮件等)。手动运行 URL 是可行的。

然后我输入我的邮件地址MAILTO并收到邮件。在邮件中,我收到了页面的整个 HTML 源代码。邮件中的预期内容是什么?为什么我的功能不起作用?

更新
如果我将我的 cron 命令更改为

 #(Updating tutor activities) - every minute
    * * * * * /usr/bin/wget http://project/cron/tutor_activities.php

仍然没有成功,这在我的邮件中 -

--15:03:01--  http://project/cron/tutor_activities.php
          => `tutor_activities.php'
Resolving project... IP Address
Connecting to test.project|IP Address|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://project./ [following]
--15:03:01--  http://project./
          => `index.html.1'
Resolving project.... IP Address
Connecting to project.|IP Address|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://project/home/ [following]
--15:03:01--  http://project/home/
          => `index.html.1'
Resolving project... IP Address
Connecting to wproject|IP Address|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
index.html.1 has sprung into existence.
Retrying.

我的项目的根目录中积累了index.html.1很多文件。index.html.2我不希望创建这些文件。只希望文件执行。

如果我使用两个命令中的任何一个,结果相同 -

   * * * * * /usr/bin/wget http://project/cron/tutor_activities.php

   * * * * * wget http://project/cron/tutor_activities.php

使用 set运行php命令MAILTO向我发送此错误 /bin/sh: php: command not found。

* * * * * php /path/to/test.php

所以,我无法使用php命令。

mailto()在 test.php 中写了一个简单的内容。通过 cron 运行时邮件不会发送(同时使用wgetphp失败),但手动运行 URL 可以正常工作。

我的问题
再次说明,我的主要问题是 cron 文件中的功能没有运行。创建文件是次要问题。

任何帮助,将不胜感激

谢谢,
桑迪潘

4

7 回答 7

7

如果您想将 url 称为 cronjob,则必须使用wget之类的东西。如果它是您服务器上的 php 脚本,它会更容易使用php /...pathtomyscript.../cron/tutor_activities.php

于 2010-10-08T13:55:31.783 回答
7

尝试

哪个php

返回的路径应该和运行 Cron 文件的命令放在一起试图运行一个 php 页面。

/path/to/php /path/to/cron/script/

如果问题仍然存在,请尝试像这样给出您的命令;请随时讨论。

于 2010-10-18T08:10:48.110 回答
1

当您使用-O -调用 wget 时,它会将下载的内容发送到标准输出,cron 通过电子邮件将其发送给您。在第一种情况下,它正在做它应该做的事情。

当您在没有-O参数的情况下调用 wget 时,它会尝试将下载的内容保存为与正在下载的网页同名的文件。如果它存在,它会将增量器添加到名称中,如您所见。在第二种情况下,它正在做它应该做的事情。

从您的问题中不清楚您希望输出到哪里,但是如果您想每次都将输出保存到同一个文件,请使用-O myfilename.html

于 2010-10-08T15:16:22.783 回答
1

如果您从 cron/命令行运行 PHP,请确保将完整路径放入 php 可执行文件

于 2010-10-12T10:18:07.530 回答
1

PHP 完全有可能不在 cron 环境中的路径中 - 它绝对不会具有与常规 shell 相同的设置。尝试在 cron 命令中使用 php 解释器和 php 脚本的绝对路径:

* * * * * /path/to/php /path/to/test.php

至于文件的创建,您只需将重定向添加到您的 wget 命令:

wget -O - ... http://....     > /dev/null

-O -强制 wget 将下载的任何内容写入标准输出,然后 cron 会很高兴地通过电子邮件发送给您。通过> /dev/null在命令末尾添加 ,此输出将转而转到天空中的 Great Bitbucket。如果您也不希望通过电子邮件发送 wget 的 stderr 输出,您还可以在2&>1之后添加一个/dev/null,这会进一步将 stderr 重定向到 stdout,它现在将转到 /dev/null。

于 2010-10-15T22:18:18.250 回答
1

我自己发现了问题。我没有在手动运行的 crontab 文件中放入相同的 URL,这是我的错误。

While running manually I was just typing test in the URL, my browsers's list of saved URLs was appearing and I was selecting the URL http://www.test.project.com/cron/tutor_activities.php, but in the crontab file I had put http://test.project.com/cron/tutor_activities.php. I was mistakenly assuming this would run http://www.test.project.com/cron/tutor_activities.php (because we have a rewrite rule present to add www)

But the rewrite rule was redirecting it to http://www.test.project.com/home. That's why the HTML content in the mails.

So, the most important thing to learn here is to make sure we don't miss the minute things and don't assume that we did everything correctly. In my case, better to copy-paste the working URL into the cron file.

于 2010-10-18T08:13:51.650 回答
0

一种简单且安全(无 tmp 文件)的方法是使用 bash 的进程替换:

* * * * * bash -c "/path/to/php <(/usr/bin/wget -O - -q -t 1 http://project/cron/tutor_activities.php)"

进程替换在其中运行命令<()并将输出放入仅在当前进程中可见的文件对象中。为了从 cron 中使用它,直接调用 bash 并将其作为命令字符串传递。

正如其他人所提到的,使用 php 的完整路径,您可以使用which php.

于 2010-10-16T19:51:49.253 回答