You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP执行Shell脚本后显示生成文件内容的异常问题

Hey there! Let's work through this issue step by step. The problem you're facing—where the text.txt content doesn't update after running your shell script via a PHP button—usually boils down to permissions, PHP not waiting for the script to finish, or caching issues. Here's how to fix it:

1. Make PHP Wait for the Shell Script to Complete

By default, some PHP execution functions might not wait for long-running scripts to finish. Use exec() with output/return code tracking to ensure PHP waits until new_run.sh is done generating the file:

<?php
// Disable browser caching to avoid stale content
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

if (isset($_POST['run_script'])) {
    $script_path = '/home/anas/kaldi/egs/voxforge/s5/new_run.sh';
    $output = [];
    $return_code = 0;

    // Capture both stdout and stderr to debug failures
    exec("$script_path 2>&1", $output, $return_code);

    // Check if the script ran successfully
    if ($return_code !== 0) {
        echo "<p>Script failed with return code $return_code:</p>";
        echo "<pre>" . htmlspecialchars(implode("\n", $output)) . "</pre>";
    }
}

// Read the latest file content
$result_file = '/home/anas/kaldi/egs/voxforge/s5/results/text.txt';
$content = '';
if (file_exists($result_file)) {
    // Clear PHP's file cache to ensure we get the latest version
    clearstatcache(true, $result_file);
    $content = file_get_contents($result_file);
}
?>

<form method="post">
    <button type="submit" name="run_script">Run Kaldi Script</button>
</form>

<div style="margin-top: 20px;">
    <h3>Latest Results:</h3>
    <pre><?php echo htmlspecialchars($content); ?></pre>
</div>
2. Fix Permissions (Critical!)

The user running PHP (usually www-data for Apache or nginx for Nginx) needs access to your script and output directory:

  • Give the script execution permissions:
    chmod +x /home/anas/kaldi/egs/voxforge/s5/new_run.sh
    
  • Ensure the PHP user can read/write to the results directory:
    # Option 1: Change ownership to the PHP user
    sudo chown -R www-data:www-data /home/anas/kaldi/egs/voxforge/s5/results/
    
    # Option 2: Open permissions (less secure, but quick for testing)
    sudo chmod -R 775 /home/anas/kaldi/egs/voxforge/s5/results/
    
  • Also verify that new_run.sh doesn't rely on commands only your user (anas) can access—add full paths to any binaries in the script if needed.
3. Verify the Script Generates the File Correctly

First, run the script manually as your user to confirm it updates text.txt:

/home/anas/kaldi/egs/voxforge/s5/new_run.sh

Check if text.txt has fresh content after running. If it doesn't, the issue is with the shell script itself, not PHP.

4. Debug with File Modification Time

Add this code right after reading the file to confirm if the file is actually being updated:

if (file_exists($result_file)) {
    clearstatcache(true, $result_file);
    echo "<p>File last modified: " . date("Y-m-d H:i:s", filemtime($result_file)) . "</p>";
    $content = file_get_contents($result_file);
}

If the timestamp doesn't change after clicking the button, the script isn't executing properly—go back to checking permissions or script output errors.

内容的提问来源于stack exchange,提问作者user7401447

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:26:06