Cronjob 发送电子邮件“命令失败,退出状态 1”当一切都没有失败时

发布于 2024-11-19 09:08:02 字数 916 浏览 1 评论 0原文

我最近一直在尝试 cronjobs,并将其设置如下:

crontab:

SHELL=/bin/sh
[email protected]
26 11 * * * wget http://diruser:[email protected]/path/to/file.php

在它运行的 php 文件中是一个 SQL 查询,目前仅运行插入。这工作正常并运行插入,但是当我查看它生成的电子邮件时,它说:

电子邮件:

wget http://diruser:[电子邮件受保护]/path/to/file.php

命令失败,退出状态为 1

我只是想知道是否有人知道为什么它会返回命令失败?它显然没有失败,但电子邮件回复让我觉得我在某个地方做错了。

另外,将 cron 正在调用的文件放入受密码保护的目录中是否是一种好的做法,或者是否有更好的方法来解决它(例如检查请求 ip 是否是服务器的 IP 或其他内容)。

I've been experimenting with cronjobs recently and have it setup like so:

crontab:

SHELL=/bin/sh
[email protected]
26 11 * * * wget http://diruser:[email protected]/path/to/file.php

Within the php file it runs is a SQL query which at the moment just runs an insert. This works fine and runs the insert but when I look at the email it produces it says this:

email:

wget http://diruser:[email protected]/path/to/file.php:

Command failed with exit status 1

I was just wondering if anyone knows why it would return command failed? It obviously doesn't fail but the email response makes me think I've done something wrong somewhere.

Also, is it good practice to put the file the cron is calling inside a password protected directory or is there a better way around it (such as checking that the request ip is that of the server or something).

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

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

发布评论

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

评论(3

水染的天色ゝ 2024-11-26 09:08:02

最有可能的是,wget 会抛出错误,因为它无法保存结果。默认情况下,wget 将尝试将结果存储在磁盘上。在 cron 作业中运行时,工作目录(通常)为 /。作为用户,您可能无法在那里存储文件。

相反,告诉 wget 删除结果,或通过管道将其传输到 /dev/null。狐狸的例子:

wget -O - -q http://diruser:[email protected]/path/to/file.php

Most likely, wget throws an error because it cannot save the result. By default wget will try to store thr result on disk. When running in a cron job, the working directory is (usually) /. You , as a user, probably cannot store a file there.

Instead, tell wget to drop the result, or pipe it to /dev/null. Fox example:

wget -O - -q http://diruser:[email protected]/path/to/file.php
魂归处 2024-11-26 09:08:02

我不知道为什么会产生错误。但在我看来,通过 wget 调用 PHP 并不是一个好的做法。您可以编写 PHP 脚本,该脚本无法通过 Apache(或 Nginx 或其他 HTTP 服务器)访问,并且可以从 cron 调用:

SHELL=/bin/sh
[email protected]
26 11 * * * php /path/to/file.php

更好的方法是像单独的用户一样调用它,例如 phpuser,谁将仅拥有脚本所需的权限。

I have no idea why error is produced. But in my opinion this is not good practive to call PHP via wget. You can write PHP script which will not be accessible over Apache (or Nginx or other HTTP server) and can be called from cron:

SHELL=/bin/sh
[email protected]
26 11 * * * php /path/to/file.php

And better way is to call it like an separated user, phpuser for example, who will have only permissions that script needs.

九厘米的零° 2024-11-26 09:08:02

尝试:

26 11 * * * /usr/local/bin/wget "http://diruser:[email protected]/path/to/file.php" > /dev/null 2>&1

或者

26 11 * * * /usr/bin/wget "http://diruser:[email protected]/path/to/file.php" > /dev/null 2>&1

1. crontab 需要命令的完整路径,才能运行它

2. wget 将尝试保存 file.php 的响应,如果它没有必要的权限,它将失败。这就是为什么您应该将输出重定向到文件以外的其他位置,这是通过 > 完成的/dev/null

程序的输入输出有3个标准源,即STDINSTDOUTSTDERR,编号分别为0代码>、<代码>1、<代码>2。

当您使用大于 > 重定向输出时,如果您没有明确提及要重定向哪一个,则默认为 STDOUT (1) 。因此,我们将所有 STDOUT 输出重定向到 null/trash,并将所有错误 2>&1 重定向到 STDOUT,后者将依次消失如之前的规则所​​示,放入垃圾箱。

Try:

26 11 * * * /usr/local/bin/wget "http://diruser:[email protected]/path/to/file.php" > /dev/null 2>&1

Or

26 11 * * * /usr/bin/wget "http://diruser:[email protected]/path/to/file.php" > /dev/null 2>&1

1. crontab needs the full path to the command, in order to run it

2. wget will try to save the response of the file.php, if it doesn't have the necessary permissions, it will fail. That's why you should redirect the output somewhere else, other than a file, which is accomplished by > /dev/null.

There are three standard sources of input and output for a program, i.e. STDIN, STDOUT, STDERR, respectively numbered as 0, 1, 2.

When you redirect the output by using the greater-than >, if you don't explicitly mention which one you want to redirect, the default one is STDOUT (1). Thus, we will redirect all STDOUT output to null/trash, and all errors 2>&1 to STDOUT, which in turn will go to trash, as denoted by the previous rule.

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