简体   繁体   English

从 cron 运行时 PHP 脚本未完成

[英]PHP script doesn't complete when run from cron

I have a PHP script that goes through all products in the database and looks at session files to see which products are in users' baskets.我有一个 PHP 脚本,它遍历数据库中的所有产品并查看 session 文件以查看哪些产品在用户的购物篮中。 Then it releases stock which is allocated to baskets but the sessions for those baskets have expired.然后它释放分配给篮子的库存,但这些篮子的会话已过期。 Simple enough.很简单。

For every product in the loop it prints out the product ID.对于循环中的每个产品,它都会打印出产品 ID。

If I run the script from command line:如果我从命令行运行脚本:

./release.php

It runs through all the products (~2500).它贯穿所有产品(〜2500)。 However, if the script is run by crontab, it goes up to somewhere between 150 and 300 products;但是,如果脚本是由 crontab 运行的,那么它会增加 150 到 300 个产品; then it stops without any errors.然后它停止而没有任何错误。

crontab -l
# m h  dom mon dow   command
*/3 *  *   *   *     /var/www/vhosts/example.com/release.php &> $HOME/release.log

There's nothing useful in /var/log/cron.log , either: /var/log/cron.log中没有任何用处,要么:

Jun  7 10:00:01 xxxxxxx /USR/SBIN/CRON[15751]: (root) CMD (/var/www/vhosts/example.com/release.php &> $HOME/log)

Are you sure it's stopping?你确定它停止了吗? You execute the script every 3 minutes, and on each call you "reset" the logfile.您每 3 分钟执行一次脚本,并在每次调用时“重置”日志文件。 ( >file instead of >>file ) >file而不是>>file

Do you use some kind of locking mechanism to prevent the script to start if it is already running?如果脚本已经在运行,您是否使用某种锁定机制来阻止脚本启动?

Either ensure that your script is only running one at a time or output your results to uniquely named log files (which is probably better since you'll want to be able to review past executions).要么确保您的脚本一次只运行一个,要么将您的结果 output 保存到唯一命名的日志文件中(这可能会更好,因为您希望能够查看过去的执行)。

*/3 *  *   *   *     /var/www/vhosts/example.com/release.php &> $HOME/release-$(date "+%Y%m%d%-%H%M%S").log

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

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