2017-09-02 43 views
0

我有一个PHP脚本,它创建了一个shell脚本,它在从同一个php文件中运行后运行,shell脚本生成一个注册表文件,脚本之后我需要读取它执行,再次从相同的PHP。 php读取文件,但是我认为它在文件被填充或创建之前就完成了,如果我再回到浏览器并再次执行php,那么textarea上就有内容。我试图解决它添加睡眠(),退出()函数和一些其他策略,但没有成功。下面是一些我尝试过的事情:PHP执行脚本后读取日志内容

// Creation of the shell script: Corpus alignment target to origin 
...... 
$cmd = "cwb-align-encode -r $REGDIR -D $CORPUSLOCATION$corpusname/$corpusname"._."$lang_tg.align\n\n"; 
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX); 

// Run the corpus indexation script 
$cmd = "/bin/bash $scriptfile > /dev/null 2>&1 &"; 
shell_exec($cmd); 

来自同一个PHP读取注册表文件:

// 1st try: no content at the textarea 
echo "<textarea id='txtArea'>".htmlspecialchars(file_get_contents($REGDIR.$corpusname))."</textarea>"; 

// 2nd try: no content at the textarea 
echo "<textarea id='txtArea'>".sleep(10); htmlspecialchars(file_get_contents($REGDIR.$corpusname))."</textarea>"; 

// 3rd try: no content at the textarea 
echo "<textarea id='txtArea'>".exit(); htmlspecialchars(file_get_contents($REGDIR.$corpusname))."</textarea>"; 

// 4th try: no content at the textarea 
echo "<textarea id='txtArea'>".if(filesize($REGDIR.$corpusname) != 0) { echo htmlspecialchars(file_get_contents($REGDIR.$corpusname)); } else { exit(0); sleep(10); htmlspecialchars(file_get_contents($REGDIR.$corpusname)); }."</textarea>"; 

回答

1

您正在使用创建的执行任务的一个新线程的命令行。 PHP不会等待它,因为你没有将strout引用到php(但是到/ dev/null)

所以,通过改变命令,你可以让PHP等待,从而得到你期望的结果。

现在我不知道肯定正确的命令是什么,但我会的东西开始喜欢

$cmd = "/bin/bash $scriptfile" 

也可以看看here。你想要的是与那个人想要的相反。但是它确实提供了关于命令实际执行的更多信息。

+0

是的,就这么简单取出'>的/ dev/null的2 >&1&'从shell脚本执行行。非常感谢。 –

+0

np,很高兴它的工作! – Jeffrey

0

尽管@Jeffrey给出的答案是正确的,但我意识到,如果shell脚本执行时间很短,否则您的php网页可能会过期或挂断,所以我再次尝试与另一个PHP函数:头('刷新:x'),这使它的工作正确!

所以这里就是我现在得到:

// Run the corpus indexation script 
$cmd = "/bin/bash $scriptfile > /dev/null 2>&1 &"; 
shell_exec($cmd); 

<textarea id="txtArea" rows="28"><?php if (filesize($REGDIR.$corpusname) != 0) { echo htmlspecialchars(file_get_contents($REGDIR.$corpusname)); } 
else { header('Refresh: 0.5'); htmlspecialchars(file_get_contents($REGDIR.$corpusname));} ?></textarea> 

UPDATE

又一解决方案:

do { echo htmlspecialchars(file_get_contents($REGDIR.$corpusname)); } while (filesize($REGDIR.$corpusname) == 0);