使用下面的代码,我试图在后台运行脚本。但是当在浏览器中点击 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;
}
}
最佳答案
您可以创建一个每分钟运行一次的 cronjob。提交表单后,您会将文件粘贴到您网站上的某个目录中。
让cronjob每分钟检查文件是否存在,如果文件存在,则通过shell脚本执行php脚本(如您所说的那样静默),然后删除文件。
关于php - 如何以静默模式执行脚本以摆脱浏览器挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35336948/