1

I have a PHP site that performs Cron's that are triggered during client executions instead of a Cron manager. One of the Cron's that are performed takes a few seconds to execute, and it keeps the connection between the Client and the Server open until it is complete. Although I know I can set up a Cron to be fired from the Server instead of during Client runs, I would like to know if it is possible without following that format.

So, can the PHP script send a command to Apache (or whatever server it is hosted on) to close the connection between the Client and the Server, but continue to functions (so, without exiting)?

4

3 回答 3

4

这适用于 Apache(显然不适用于带有 FastCGI 的 IIS)

<?php
ignore_user_abort(true); // make sure PHP doesn't stop when the connection closes

// fire and forget - do lots of stuff so the connection actually closes
header("Content-Length: 0");
header("Connection: Close");
flush();
session_write_close(); // if you have a session

do_processing();
// don't forget to `set_time_limit` if your process takes a while
于 2013-07-02T16:58:53.527 回答
1

您可以使用shell调用 PHP 二进制文件... 示例:

shell("php -f /path/to/cronfile.php > /dev/null 2>/dev/null &");它将运行而不是等待返回。

请参阅PHP 中的异步 shell exec

于 2013-07-02T17:00:03.323 回答
-1

通常有两种执行形式。

客户端:客户端将留在页面上,页面将继续处理给出的命令,直到完成。

服务器端:客户端将导航到一个页面,您在数据库中进行切换:

  UPDATE Table SET PendinCron=1 WHERE IdentiferCol=$IdentiferData

然后,您将以 X 间隔运行 Cronjob,并且仅在PendinCron等于 1 时才处理,如果不是。Cron 不会执行所需的任务。

于 2013-07-02T17:00:33.283 回答