简体   繁体   English

从 crontab 执行 php 脚本的正确方法

[英]the rigth way to execute a php script from crontab

I have a .php script that includes other php scripts and as final result a db is updated and a report file has been created.我有一个包含其他 php 脚本的 .php 脚本,最终结果是更新了一个数据库并创建了一个报告文件。

if I manually call it from a browser all works fine.如果我从浏览器手动调用它一切正常。

I tried to call it from the crontab with the following syntax我尝试使用以下语法从 crontab 中调用它

/usr/local/bin/php /home/PATH/myscript.php

and at the planned time, it seems to stop at the first include();并且在计划的时间,它似乎在第一个 include() 处停止; and die.和死。 I added a mail();我添加了一个邮件(); sending after each include();在每个 include() 之后发送;

If I call the file from the browser I receive all the 5 mails, when it's called from the crontab, I receive only the first email如果我从浏览器调用该文件,我会收到所有 5 封邮件,当从 crontab 调用该文件时,我只会收到第一封电子邮件

Where am I doing wrong?我哪里做错了?

*edit: I tried to call always the first script and I received all the mail. *编辑:我总是尝试调用第一个脚本并收到所有邮件。 It seems the second script contains something that is stopping the code but ONLY is it's called by the cron似乎第二个脚本包含停止代码的东西,但只有它被 cron 调用

Use this instead改用这个

php -q /home/PATH/myscript.php

As per this answer by St. Woland , the -q flag:根据St. Woland 的这个答案-q标志:

The -q flag suppresses HTTP header output. -q 标志禁止 HTTP 标头输出。

You can run a php script like this:你可以像这样运行一个 php 脚本:

php -f script.php

If you want to use another php.ini (what often happens with console apps), use如果您想使用另一个php.ini (控制台应用程序经常发生的情况),请使用

php -c path/where/php.ini -f script.php

type类型

php --help

for more options.更多选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM