bash 在 CentOS 中设置 Crontab 以执行 PHP 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18702425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Up Crontab to Execute a PHP Script in CentOS
提问by Joe Majewski
I have searched hell and high water for a solution to a problem I'm having in CentOS. I'm trying to set up a cron job that executes a PHP script. I was able to get this working using wget
, but now that we are going into production, I need to find a way to do this whilst being more secure, as the cron job itself works with sensitive data.
我已经在地狱和高水位中寻找解决我在 CentOS 中遇到的问题的方法。我正在尝试设置一个执行 PHP 脚本的 cron 作业。我能够使用 来完成这项工作wget
,但是现在我们正在投入生产,我需要找到一种方法来做到这一点,同时更安全,因为 cron 作业本身可以处理敏感数据。
The error that I'm getting is: -bash: php: command not found
.
我得到的错误是:-bash: php: command not found
。
Now I've looked around and I've seen people having the same problem, but nothing has been able to help me get this working.
现在我环顾四周,看到有人遇到同样的问题,但没有什么能帮助我解决这个问题。
For reference, here is what the working crontab looked like using the wget
command.
作为参考,这里是使用该wget
命令的工作 crontab 的样子。
* * * * * wget http://www.domain.com/cron_script.php
* * * * * wget http://www.domain.com/cron_script.php
This is working fine, but I need to translate this into executing via PHP, rather than making an HTTP request to get the job done.
这工作正常,但我需要将其转换为通过 PHP 执行,而不是发出 HTTP 请求来完成工作。
Let me know if I left anything out.
如果我遗漏了什么,请告诉我。
回答by Cfreak
Cron doesn't have it's PATH set. The easiest thing is to change the php command to the full path of the php binary.
Cron 没有设置它的 PATH。最简单的就是把php命令改成php二进制文件的完整路径。
/usr/bin/php /path/to/yourscript.php
/usr/bin/php /path/to/yourscript.php
I'm fairly certain that's the path in CentOS but you can know for sure by doing which php
on the command line and it will tell you.
我相当确定那是 CentOS 中的路径,但您可以通过which php
在命令行上进行操作来确定,它会告诉您。