如何在静默模式下执行脚本以摆脱浏览器挂起 [英] How to execute script in silent mode to get rid from browser hang on

查看:379
本文介绍了如何在静默模式下执行脚本以摆脱浏览器挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有没有想要我可以获得进程ID(Linux PID)作为过程启动时的输出。
我们可以说根据需要我想创建过程与Cron作业相同。但我不想设置一个Cron工作。因为我有1000个用户,每个用户想要按需爬行。



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



解决方案

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



让cronjob检查文件是否每分钟都有,如果文件是通过shell脚本执行一个php脚本(默认如你所说),并删除文件。


With below code i am trying to run script in background. but when hit url in browser then browser is waiting is completion and after this success message is coming. I do not want to hang on browser and do not want to wait for its complete.

Is there any want i can get Process ID (Linux PID) as output when process start. We can say on demand i want to create Process same as Cron job does. but i do not want to setup a Cron job. Because i have 1000 users and each user want to do crawl on demand.

My prev question which is related to this.

How to create background process in PHP

Code on PasteBin: http://pastebin.com/6FkS9ZYx

component:

/
 * @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;
    }
}

解决方案

You can create a cronjob which runs every minute. As soon as you submit the form you paste a file to some directory on your website.

Let the cronjob check if the file is there every minute, If the file is there execute a php script (silently as you say) through a shell script, and remove the file.

这篇关于如何在静默模式下执行脚本以摆脱浏览器挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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