doujing1967
doujing1967
2017-09-02 10:57

Php在执行脚本后读取日志内容

已采纳

I have a php script that creates a shell script which is run after making it from the same php file, the shell script generates a registry file that I need to read after the script is executed, again from the same php. The php reads the file, but I think it does it before the file is filled or created, if I go back at the browser and execute the php again, then there is content at the textarea. I have tried to solve it adding sleep(), exit() functions and some other strategies but no success. Here are some of the things I've tried:

// Creation of the shell script: Corpus alignment target to origin
......
$cmd = "cwb-align-encode -r $REGDIR -D $CORPUSLOCATION$corpusname/$corpusname"._."$lang_tg.align

";
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);

Read the registry file from the same 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>";
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doulv1760 doulv1760 4年前

    The command line you are using created a new thread that performs the task. PHP wont wait for it, as you do not refer the strout to php (but to /dev/null)

    So, by changing the command, you can make PHP wait and thus get the result you expect.

    Now I don't know for sure what the correct command is, but I would start with something like

    $cmd = "/bin/bash  $scriptfile"
    

    Also have a look here. You want the opposite of what that guy wants. It does however give a bit more information about what the command actually does.

    点赞 评论 复制链接分享
  • dongrunying7537 dongrunying7537 4年前

    Even though the answer given by @Jeffrey is the right one, I realized that this answer is only good enough if the shell script takes short execution time, otherwise your php webpage can expire or hang up, so I gave another try with another php function: header('Refresh: x'), and that made it work right!

    So here's what I get now:

    // 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

    Yet another solution:

    do { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); } while (filesize($REGDIR.$corpusname) == 0);
    
    点赞 评论 复制链接分享

相关推荐