检查 cron 作业是否正确运行脚本 - 在批处理中记录错误的正确方法

发布于 2024-12-21 18:59:39 字数 789 浏览 3 评论 0原文

我已经设置了一个 cronjob 每天运行一个脚本。该脚本从数据库中提取 Id 列表,循环遍历每个 Id 以从数据库中获取更多数据,并根据检索到的数据生成 XML 文件。

前几天似乎运行良好,但是,ID 列表越来越大,今天我注意到并非所有 XML 文件都已生成。看来是随机ID没有运行。我已手动运行脚本来为某些缺失的 ID 单独生成 XML,并且它们运行时没有任何问题。

我不确定如何定位问题,因为 cron 作业肯定正在运行,但并不总是生成所有 XML 文件。关于如何查明此问题并快速找出哪些文件尚未运行的任何想法。

我想也许可以将 timestarttimeend 字段添加到数据库中,并在每个正在运行的 XML 生成器的开始和结束处输入这些值,这样我就可以看到运行了什么以及什么都没有,但想知道是否有更好的方法。

set_time_limit(0);

//connect to database
$db = new msSqlConnect('dbconnect');

$select = "SELECT id FROM ProductFeeds WHERE enabled = 'True' ";

$run = mssql_query($select);
while($row = mssql_fetch_array($run)){

    $arg = $row['id'];
    //echo $arg . '<br />';
    exec("php index.php \"$arg\"", $output);
    //print_r($output);

}

I have set up a cronjob to run a script daily. This script pulls out a list of Ids from a database, loops through each to get more data from the database and geneates an XML file based on the data retrieved.

This seems to have run fine for the first few days, however, the list of Ids is getting bigger and today I have noticed that not all of the XML files have been generated. It seems to be random IDs that have not run. I have manually run the script to generate the XML for some of the missing IDs individually and they ran without any issues.

I am not sure how to locate the problem as the cron job is definately running, but not always generating all of the XML files. Any ideas on how I can pin point this problem and quickly find out which files have not been run.

I thought perhaps add timestart and timeend fields to the database and enter these values at the start and end of each XML generator being run, this way I could see what had run and what hadn't, but wondered if there was a better way.

set_time_limit(0);

//connect to database
$db = new msSqlConnect('dbconnect');

$select = "SELECT id FROM ProductFeeds WHERE enabled = 'True' ";

$run = mssql_query($select);
while($row = mssql_fetch_array($run)){

    $arg = $row['id'];
    //echo $arg . '<br />';
    exec("php index.php \"$arg\"", $output);
    //print_r($output);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆依然 2024-12-28 18:59:39

我的建议是在脚本中添加一些日志记录。一个简单的

error_log("Passing ID:".$arg."\n",3,"log.txt");

可以给你一些关于ID是否被传递的信息。如果您发现这种情况,您可以将日志记录引入到index.php以进一步评估问题。

顺便说一句,你能解释一下为什么使用 exec() 来运行 php 脚本吗?为什么不在循环中执行函数。这很可能是问题的根源。

因为使用 exec 我认为该进程将在后台运行并且循环将继续,所以你真的可以用这种方式阻塞你的服务器,也许这也值得尝试。 (我认为这也取决于输出的方式:

注意:如果程序使用此函数启动,为了使其继续在后台运行,程序的输出必须重定向到文件或另一个输出流。如果不这样做将导致 PHP 挂起,直到程序执行结束。

也许其他一些用户可以对此发表评论。

My suggestion would be to add some logging to the script. A simple

error_log("Passing ID:".$arg."\n",3,"log.txt");

Can give you some info on whether the ID is being passed. If you find that that is the case, you can introduce logging to index.php to further evaluate the problem.

Btw, can you explain why you are using exec() to run a php script? Why not excute a function in the loop. This could well be the source of the problem.

Because with exec I think the process will run in the background and the loop will continue, so you could really choke you server that way, maybe that's worth trying out as well. (I think this also depends on the way of outputting:

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

Maybe some other users can comment on this.

得不到的就毁灭 2024-12-28 18:59:39

结果发现 apache 超时了。因此,与使用函数或 exec() 函数无关。

Turned out the apache was timing out. Therefore nothing to do with using a function or the exec() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文