Php 如何在静默模式下执行脚本以摆脱浏览器挂起
使用下面的代码,我试图在后台运行脚本。但当在浏览器中点击url时,浏览器正在等待的是完成,在这个成功消息出现之后。 我不想挂断浏览器,也不想等待其完成 是否需要在进程启动时获取进程ID(Linux PID)作为输出。 我们可以说,按需创建的流程与Cron作业相同。但我不想设置Cron作业。因为我有1000个用户,每个用户都想按需爬网 我的上一个问题与此相关 PasteBin上的代码: 组件:Php 如何在静默模式下执行脚本以摆脱浏览器挂起,php,linux,cakephp,command-line-interface,Php,Linux,Cakephp,Command Line Interface,使用下面的代码,我试图在后台运行脚本。但当在浏览器中点击url时,浏览器正在等待的是完成,在这个成功消息出现之后。 我不想挂断浏览器,也不想等待其完成 是否需要在进程启动时获取进程ID(Linux PID)作为输出。 我们可以说,按需创建的流程与Cron作业相同。但我不想设置Cron作业。因为我有1000个用户,每个用户都想按需爬网 我的上一个问题与此相关 PasteBin上的代码: 组件: / * @component to write sample script which will p
/
* @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;
}
}
/
*@component编写将传递给后端作业的示例脚本
*
*/
应用程序::使用(‘组件’、‘控制器’);
类BackgroundScriptComponent扩展组件{
公共函数crawlurls($crawling){//传递500个URL进行爬网
对于($i=0;$i<$Counter;$i++){
/*
使用浏览器hit运行完美的内部代码
*/
}
}
}
/*@component编写将传递给后端作业的示例脚本*/
控制器:
类ScriptController扩展AppController{
public$components=array('BackgroundScript');
公共函数CreateBackendProcess(){
$this->layout=false;
$this->autoRender=false;
$arrOfUrls=array('此数组中有500个url');
//echo$this->BackgroundScript->crawlurls($crawling);//此行具有组件方法调用。我不希望调用它与..相同。它应该从CLI调用
$cmd=”“;//这个$cmd中应该包含什么来调用BackgroundScript的crawlurls()方法组件,该组件将传递给exec()
执行官($cmd,$pid);
echo“
使用进程id运行的脚本”。json_encode($pid);//我将把这个PS id保存到数据库中
出口
}
}
您可以创建一个每分钟运行的cronjob。一旦你提交了表格,你就会将一个文件粘贴到你网站的某个目录中
让cronjob检查文件是否每分钟都在那里,如果文件在那里,则通过shell脚本执行一个php脚本(如您所说的那样,以静默方式),然后删除该文件。您可以创建一个每分钟运行的cronjob。一旦你提交了表格,你就会将一个文件粘贴到你网站的某个目录中
让cronjob每分钟检查一次文件是否在那里,如果文件在那里,则通过shell脚本执行一个php脚本(如您所说的那样,以静默方式),然后删除该文件。对不起。我不能每分钟都运行Cron作业。。你能提供其他解决方案吗?在过去的5天里,我一直在努力解决这个问题。为什么你不能每分钟运行一个cronjob呢?我也不想要cronjob,因为可能有1000个用户,每个用户都会运行包含50多个URL的脚本。是的,有一个可选的
passthru(“/usr/bin/php/path/to/yourFile.php>/path/to/log\u file.log 2>&1&;”代码>确定。它会等到输出吗?否则它会回来让我继续走对不起。我不能每分钟都运行Cron作业。。你能提供其他解决方案吗?在过去的5天里,我一直在努力解决这个问题。为什么你不能每分钟运行一个cronjob呢?我也不想要cronjob,因为可能有1000个用户,每个用户都会运行包含50多个URL的脚本。是的,有一个可选的passthru(“/usr/bin/php/path/to/yourFile.php>/path/to/log\u file.log 2>&1&;”代码>确定。它会等到输出吗?否则它会回来让我继续前进