简体   繁体   English

PHP监控程序

[英]php monitor process

I'm sure similar questions have bee answered over and over again. 我确信类似的问题已经一遍又一遍地回答了。 If yes then I googled in the wrong direction and apologize for that. 如果是,那么我在错误的方向上进行了搜索,并为此表示歉意。

My problem: I'm writing a web page with a process running in the background. 我的问题:我正在编写一个在后台运行的进程的网页。 The process I'm talking about is a R script which runs quite long maybe several days. 我正在谈论的过程是一个R脚本,它运行时间很长,也许几天。 When the progress is started, its started like that. 当进度开始时,就这样开始了。

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

Id' like to track whether the process is still running. 想要跟踪该过程是否仍在运行。 This way, when the user checks he gets either the message that it is finished or not. 这样,当用户检查他是否收到消息已完成或未完成时。 The tracking starts and stops when the user chooses the input file he uploaded on to the server after for example he logs in again or did something else on the page. 当用户选择例如他再次登录或在页面上做了其他操作之后选择上载到服务器的输入文件时,跟踪将开始和停止。 I also want it to update the page when the process finishes so the page changes in case he is just looking at it. 我也希望它在过程完成时更新页面,以便页面更改,以防他只是看着它。

I have two possibilities. 我有两种可能性。 I can either check it via the process id or whether an output file is generated or not. 我可以通过进程ID或是否生成输出文件来检查它。

I tried 我试过了

while(is_process_running($ps)){ ob_flush(); flush(); sleep(1); }

this works kind of, except that all other functionality on the page freezes. 除了页面上的所有其他功能都冻结之外,这种方法行之有效。 That's my is_process_running($ps) function. 那就是我的is_process_running($ ps)函数。

function is_process_running($PID){ exec("ps $PID", $ProcessState); return(count($ProcessState) >= 2); }

What I really need is another process running in the background checking whether the first process is still running and if not, refreshes the page when the first process finishes. 我真正需要的是在后台运行的另一个进程,以检查第一个进程是否仍在运行,如果没有运行,则在第一个进程完成时刷新页面。 How would you do that? 你会怎么做? Please let me know if you need additional information. 如果您需要其他信息,请告诉我。 All help is much appreciated. 非常感谢所有帮助。

First off, there are a number of issues with the way you are starting the process - it really needs to be a in a separate process group from the PHP which launches it. 首先,启动过程的方式存在很多问题-实际上,它需要与启动它的PHP分开在一个单独的过程组中

As to checking its status, again, you don't want PHP stuff hanging around for a long time at the end of an http connection. 至于检查其状态,同样,您不希望PHP的内容在http连接结束时长时间徘徊。 Also getting the webserver to flush content to the browser on demand AND getting the browser to render it progressively is very difficult - and fragile. 同样,让网络服务器按需将内容刷新到浏览器并让浏览器逐步呈现它是非常困难的,而且非常脆弱。 Even if you get it working on one browser/webserver combination it's unlikely to work in another. 即使您将其用于一种浏览器/网络服务器组合,也不太可能在另一种浏览器/网络服务器组合中使用。

Use Ajax to poll a simple script which reports back on the process status / progress. 使用Ajax轮询一个简单的脚本,该脚本将报告过程状态/进度。

with symcbean's answer I was able to solve it. 有了symcbean的回答,我得以解决。 My javascript code is below maybe its of use to someone who faces the same problem. 我的javascript代码在下面可能对遇到相同问题的人有用。 I'm also open for improvement. 我也愿意改进。 I still consider myself a beginner. 我仍然认为自己是初学者。

 function loadXMLDoc(){ var xmlhttp; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else{// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function start(file,time){ var xmlhttp = loadXMLDoc(); if(lines == 0){ timer = setInterval(function(){sendRequest(xmlhttp,file)},time); } } function sendRequest(xmlhttp,file){ xmlhttp.open("POST",file,true); xmlhttp.send() xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200 && xmlhttp.responseText != ""){ var text = xmlhttp.responseText; var splitted = text.split("\\n"); var length = splitted.length if(splitted[length-2]=="finished"){ refresh_page(); clearInterval(timer); lines = 0; } } } } 

The start method is called with file path and the time interval its supposed to check for changes. 使用文件路径和应该检查更改的时间间隔调用start方法。 Note the refresh page method I did not post but that just whatever you want to refresh on your page. 请注意,我没有发布刷新页面方法,但是您想刷新页面上的任何内容。 The page is refreshed when the last line in the file says finished. 该页面被刷新时,文件中的最后一行说结束了。 I included the line variable to check whether the process is already started or not. 我包含了line变量,以检查该进程是否已经启动。 I have not fully tested the code but so far its doing what I want it to do. 我还没有完全测试代码,但到目前为止,它已经完成了我想要的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM