How do I setup a php script to run in crontab?

我该如何设置一个PHP脚本运行crontab?

Tags: php crontab
标签: php crontab

问题 (Question)

I am trying to get a php script to run in crontab but when run in crontab I get an include error from php.

The crontab entry looks something like:

* * * * * php /path/sendEmail.php > /path/logs/sendEmail/sendEmail-`date +\%Y-\%m-\%H\ \%k:\%M:\%S`.log 2>&1 &

This creates the log files however inside the log files I get some php errors:

PHP Warning:  include_once(../../../lib/Database.php): failed to open stream: No such file or directory in /path/sendEmail.php on line 6

Warning: include_once(../../../lib/Database.php): failed to open stream: No such file or directory in /path/sendEmail.php on line 6
PHP Warning:  include_once(): Failed opening '../../../lib/Database.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /path/sendEmail.php on line 6

Warning: include_once(): Failed opening '../../../lib/Database.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /path/sendEmail.php on line 6
PHP Fatal error:  Class 'Database' not found in /path/sendEmail.php on line 9

Fatal error: Class 'Database' not found in /path/sendEmail.php on line 9

When I run the command in the crontab entry I get the same errors in the log file. Now when I run php sendEmail.php in the terminal it works perfectly fine. Emails send out and everything. In the previous cases I get the errors (and the emails also don't send out).

Not sure how piping the output seems to change how the php script is run. Any ideas on how to get a php script to run as a cronjob?

我想得到一个PHP脚本运行crontab但当运行crontab我得到一个包含错误从PHP。

crontab条目看起来像:

* * * * * php /path/sendEmail.php > /path/logs/sendEmail/sendEmail-`date +\%Y-\%m-\%H\ \%k:\%M:\%S`.log 2>&1 &

这将创建日志文件,但是在我得到一些PHP错误日志文件:

PHP Warning:  include_once(../../../lib/Database.php): failed to open stream: No such file or directory in /path/sendEmail.php on line 6

Warning: include_once(../../../lib/Database.php): failed to open stream: No such file or directory in /path/sendEmail.php on line 6
PHP Warning:  include_once(): Failed opening '../../../lib/Database.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /path/sendEmail.php on line 6

Warning: include_once(): Failed opening '../../../lib/Database.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /path/sendEmail.php on line 6
PHP Fatal error:  Class 'Database' not found in /path/sendEmail.php on line 9

Fatal error: Class 'Database' not found in /path/sendEmail.php on line 9

当我运行命令在crontab条目我在日志文件中得到同样的错误。现在当我跑php sendEmail.php在终端工作的非常好。邮件发送和一切。在以前的情况下,我得到的错误(和电子邮件也不发出)。

不知道管道输出似乎改变了PHP脚本运行。任何想法如何让一个PHP脚本的运行作为一个任务?

最佳答案 (Best Answer)

When cron starts the script its working directory is very unlikely to be the same directory in which your PHP script lives, so relative paths like ../../../lib/Database.php are inadvisable, at least in their current form.

To the top of your script add:

define('__SCRIPT_ROOT', dirname(__FILE__));

And change your includes to:

include_once(__SCRIPT_ROOT . '/../../../lib/Database.php');

Now __SCRIPT_ROOT contains the absolute path to the script, and you can use that as a base to perform the relative pathing.

当cron启动脚本的工作目录是在你的PHP脚本的生活同一目录非常不可能的,所以相对路径一样../../../lib/Database.php是不可取的,至少在目前的形式。

你的脚本的顶部添加:

define('__SCRIPT_ROOT', dirname(__FILE__));

更改包括:

include_once(__SCRIPT_ROOT . '/../../../lib/Database.php');

现在天气__SCRIPT_ROOT包含绝对路径的脚本,您可以使用为基础进行的相对路径。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22082586