php - 如何以静默模式执行脚本以摆脱浏览器挂起

标签 php linux cakephp command-line-interface

使用下面的代码,我试图在后台运行脚本。但是当在浏览器中点击 url 时,浏览器正在等待完成,并且在此成功消息之后。 我不想卡在浏览器上,也不想等待它完成。

是否需要在进程启动时将进程 ID (Linux PID) 作为输出。 我们可以按需说我想创建与 Cron 作业相同的流程。但我不想设置 Cron 作业。因为我有 1000 个用户,每个用户都想按需进行爬网。

我的上一个问题与此相关。

How to create background process in PHP

PasteBin 上的代码:http://pastebin.com/6FkS9ZYx

组件:

/
 * @component to write sample script which will pass to backend jobs 
 *
 */

App::uses('Component', 'Controller');
class BackgroundScriptComponent extends Component {

    public function crawllUrls($crawling) {   // Passing 500 urls to crawl

        for ($i = 0; $i < $Counter; $i++) {

            /*
                Internal code which is running perfect using browser hit
            */

        }
    }
}

/* @component to write sample script which will pass to backend jobs */

Controller:

class ScriptController extends AppController {

    public $components = array('BackgroundScript');

    public function CreateBackendProcess() {
        $this->layout = false;
        $this->autoRender = false;

        $arrOfUrls = array('500 url there in this array');

        //echo $this->BackgroundScript->crawllUrls($crawling);  // This line having Component method call. I do not want to call this as same as.. It should call from CLI

        $cmd = "";    //what should come in this $cmd to calls BackgroundScript's crawllUrls() method component which will pass to exec() 

        exec($cmd, $pid);

        echo "<br />Script Running with process id " . json_encode($pid); //I will save this PS ID to database
        exit;
    }
}

enter image description here

最佳答案

您可以创建一个每分钟运行一次的 cronjob。提交表单后,您会将文件粘贴到您网站上的某个目录中。

让cronjob每分钟检查文件是否存在,如果文件存在,则通过shell脚本执行php脚本(如您所说的那样静默),然后删除文件。

关于php - 如何以静默模式执行脚本以摆脱浏览器挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35336948/

相关文章:

php - 如何在 PHP 中的同一 mysqli_query 中使用 for 循环、IN 运算符

php - Mysql查询查找不同列的计数

linux - unix awk 命令没有列出多个匹配的模式

jquery - cakephp 2 ajax 形式

php - 我可以隐藏 Controller 并查看名称 cake php 2 吗?

php - 选择并显示表中最后 5 个条目的数据

php - 如何处理图像 blob 列 mySQL PHP

linux - 如何从 AIX 6.1 使用用户名和密码登录 ftp?

linux - 如何在 bash 中一次对两行进行排序,使用第二行作为索引?

php - 从 Array Multi 访问数据