2017-09-02 15 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);