Cron 正在运行但功能不起作用

发布于 2024-09-26 08:09:56 字数 2190 浏览 3 评论 0原文

我有几个 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.1index.html.2 文件会累积在我的项目的根。我不想创建这些文件。只是想让文件执行。

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

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

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

使用 MAILTO 设置运行 php 命令会向我发送此错误 /bin/sh: php: command not find。

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

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

我在 test.php 中编写了一个简单的 mailto() 。通过 cron 运行时不会收到邮件(使用 wgetphp 都失败),但手动运行 URL 可以。

我的问题
再次澄清一下,我的主要问题是 cron 文件内的功能没有运行。文件的创建是次要问题。

任何帮助将不胜感激

,谢谢,
桑迪潘

I have several PHP files to be run by cron. I set up the crons using command-

crontab crontab.txt

Inside the crontab.txt file, I have written cron commands like this:-

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

But none of the functionalities are working (database queries, sending reminder mails etc.). Running the URLs manually works.

Then I put my mail address in MAILTO and received the mails. In the mail, I received entire HTML source of the page. What is expected in the mail? Why are my functionalities not working?

Updates
If I change my cron commands to

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

Still no success and this comes in my mail -

--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.

And lots of index.html.1 , index.html.2 files are accumulating in the root of my project. I do not want these files to be created. Just want the files to execute.

Same results if I use either of the two commands -

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

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

running php command with MAILTO set sends me this error /bin/sh: php: command not found.

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

So, I am not able to use php command.

I have written a simple mailto() inside my test.php. The mail does not come when run through cron (using both wget and php fails) but running the URL manually works.

My problem
To make it clear again, my main problem is that the functionality inside the cron files is not running. Creation of files is a secondary issue.

Any help would be appreciated

Thanks,
Sandeepan

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

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

发布评论

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

评论(7

九局 2024-10-03 08:09:56

如果你想调用一个 url 作为 cronjob,你必须使用类似 wget 的东西。如果它是您服务器上的 php 脚本,那么使用 php /...pathtomyscript.../cron/tutor_activities.php 会更容易

if you want to call an url as cronjob, you'll have to use somthing like wget. if it's a php-script on your server it would be easier to use php /...pathtomyscript.../cron/tutor_activities.php

地狱即天堂 2024-10-03 08:09:56

尝试

哪个 php

返回的路径应该与运行 Cron 文件所传递的命令放在一起。如果您通过 Shell 设置 Cron,则不会出现任何问题,但请放心,尝试在以下情况下给出绝对路径您正在尝试运行一个 php 页面。

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

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

try

which php

The path which is returned should be placed with the command which is passed to run the Cron file.If you are setting up the Cron through Shell,it won't give any problem,but to be assured,try giving absolute path when you are trying to run a php page.

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

Try to give your comand like this,if the problem persists;feel free to discuss.

遇见了你 2024-10-03 08:09:56

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

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

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

When you call wget with -O -, it will send the downloaded content to stdout, which cron is sending to you via the email message. In the first case, it's doing exactly what it should.

When you call wget witout the -O parameter, it will try to save the downloaded content as a file of the same name as the web page being downloaded. If it exists, it will add the incrementer to the name, as you saw. In this second case, it's doing exactly what it should.

It's not clear from your question where you want the output to go, but if you want to save the output to the same file each time, use -O myfilename.html.

山川志 2024-10-03 08:09:56

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

If your running PHP from cron/command line make sure you put the full path to the php executable

伪心 2024-10-03 08:09:56

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

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

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

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

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

It's entirely possible that PHP's not in the path within the cron environment - it's definitely not going to have the same setup as your regular shell. Try using the absolute path to BOTH the php interpreter AND the php script in the cron command:

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

As for the creation of files, you just have to add a redirect to your wget command:

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

-O - forces wget to write anything it downloads to standard output, which cron will then happily email to you. By adding the > /dev/null at the end of the command, this output will instead go the Great Bitbucket in the Sky. If you don't want wget's stderr output emailed either, you can also add a 2&>1 after the /dev/null, which further redirects stderr to stdout, which is now going to /dev/null.

隐诗 2024-10-03 08:09:56

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

手动运行时,我只是在 URL 中输入 test,浏览器的已保存 URL 列表出现,并且我选择了 URL http://www.test.project.com/cron/tutor_activities.php ,但在 crontab 文件中我放置了 http://test.project.com/cron/tutor_activities.php。我错误地认为这会运行 http://www.test.project.com/cron/tutor_activities.php (因为我们有一个重写规则来添加 www

但重写规则将其重定向到http://www.test.project.com/home。这就是邮件中包含 HTML 内容的原因。

因此,在这里要学习的最重要的事情是确保我们不会错过细节,并且不要假设我们所做的一切都是正确的。就我而言,最好将工作 URL 复制粘贴到 cron 文件中。

I found the problem myself. I did not put the same URL in my crontab file which I was running manually and that was my mistake.

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.

篱下浅笙歌 2024-10-03 08:09:56

一种简单且安全(无 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 找到它。

An easy and secure (no tmp files) way to do this is to use bash's process substitution:

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

Process substitution runs the command within <() and puts the output into a file object that is only visible from the current process. In order to use it from cron, invoke bash directly and pass it as a command string.

And as others have mentioned, use the full path to php which you can find out with which php.

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