[英]How can I read a file line by line for 10 seconds?
我正在做一個項目,需要閱讀日志並檢查錯誤。 我正在使用while循環,如下所示:
while($count < 10) {
$count++;
# Stop waiting if an exception is thrown
$sdata = file($logfile);
$lastline = $sdata[count($sdata)-1];
if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; }
sleep(1);
}
這是每秒檢查一次日志,但是,我需要讀取它的每一行。在保持while循環的超時值的同時,如何讀取寫入日志的最后一行?
這會滿足您的需求嗎?
$time = time();
$stop = $time+10;
while($time < $stop) {
$time = time();
# Stop waiting if an exception is thrown
$sdata = file($logfile);
$lastline = end($sdata);
if(startsWith($lastline, "Running:") == false or startsWith($lastline,"Executing:") == false) { break; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.