PHP脚本从命令行工作,而不是从cron工作 [英] PHP script works from command line, not from cron

查看:197
本文介绍了PHP脚本从命令行工作,而不是从cron工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CakePHP脚本,应该希望由cron作业运行。它从命令行运行良好,但似乎不是从cron。 cron行是这样的:

I have a CakePHP script that should hopefully be run by a cron job. It runs fine from the command line, but seemingly not from the cron. The cron line is something like:

*/2 * * * * cd /path/to/app;../cake/console/cake do_update

脚本本身 - 这是我认为可能也是wacky,通过数据库中的Realtors表的子集来抛弃cron-loops,使用系统时间来决定要更新的数据库的50条记录片:

The script itself - and this is the bit that I think may possibly be too wacky, to the extent of throwing off the cron - loops through a subset of a Realtors table in the database, using the system time to decide which 50-record slice of the database to update:

$realtors = $this->Realtor->find('all',array(
'conditions'=>array('Realtor.zone_id'=>1),
'order'=>array('Realtor.num DESC'),
'limit'=>50,
'offset'=>date("i")*25
));

所以我的问题是/是 - 有什么我在这里,抛出cron作业循环?也许更重要的是,我的方法是将数据库分成可管理的块,在一个小时的疯狂? (我几乎是一个编程新手,所以我尝试了很多事情,不知道他们是否是好的做法。)任何人都可以建议一个更好的方式循环和更新大量的数据库记录通过cron,

So my question(s) is/are - is there anything I'm doing here that would obviously throw the cron job for a loop? And, perhaps more importantly, is my method of splitting a database into manageable chunks over the course of an hour crazy? (I'm pretty much a programming newbie, so I try a lot of things without knowing whether they're good practice or not.) Can anyone suggest a better way of looping through and updating large numbers of database records via a cron, that prevents the individual queries from being too huge for the system to handle?

编辑:它不仅总是从命令行工作,它也可以运行在cron脚本在不同的服务器上。我想在特定的服务器上只有一些东西搞砸了,所以似乎怀疑有一个代码相关的解决方案!我只接受下面有用的cron相关洞察的答案...

not only does it always work from the command line, it also works when run by cron script on a different server. I guess there's just something messed up on the particular server, so it seems doubtful there's a code-related solution! I'll just accept an answer from among the useful cron-related insights below...

推荐答案

一般来说,将所有命令行文件放入shell文件

Generally, you'd want to put all your command line stuff into an shell file

/path/to/cake/console/cron.sh

/path/to/cake/console/cron.sh


#!/bin/sh
cd /path/to/app
php ../cake/console/cake do_update




*/2 * * * * sh /path/to/cake/cron.sh

通常我不在乎应用程序实际启动的位置(如果我不使用文件),只需执行

Normally I don't care about where the application actually starts (if i'm not using files) and just do


*/1 * * * * php /my/path/my_php.php

这篇关于PHP脚本从命令行工作,而不是从cron工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆