diff --git a/Server/application/command/TaskSchedulerCommand.php b/Server/application/command/TaskSchedulerCommand.php index 82af207f..4f8f09f4 100644 --- a/Server/application/command/TaskSchedulerCommand.php +++ b/Server/application/command/TaskSchedulerCommand.php @@ -482,6 +482,7 @@ class TaskSchedulerCommand extends Command $error = ''; $loopCount = 0; $lastStatusCheck = 0; + $finalExitCode = null; // 保存进程结束时的退出码 // 等待进程完成或超时 while (true) { @@ -527,7 +528,8 @@ class TaskSchedulerCommand extends Command $debugEnd = "[DEBUG] 进程已结束\n"; $debugEnd .= "[DEBUG] 最终状态: " . json_encode($status, JSON_UNESCAPED_UNICODE) . "\n"; if (isset($status['exitcode'])) { - $debugEnd .= "[DEBUG] proc_get_status 返回的退出码: " . $status['exitcode'] . "\n"; + $finalExitCode = $status['exitcode']; // 立即保存退出码 + $debugEnd .= "[DEBUG] proc_get_status 返回的退出码: " . $status['exitcode'] . " (已保存)\n"; } file_put_contents($logFile, $debugEnd, FILE_APPEND); break; @@ -579,13 +581,7 @@ class TaskSchedulerCommand extends Command // [DEBUG] 记录关闭管道前的状态 file_put_contents($logFile, "[DEBUG] 准备关闭管道,输出长度: " . strlen($output) . ", 错误长度: " . strlen($error) . "\n", FILE_APPEND); - - // 在关闭管道前,再次获取进程状态以获取准确的退出码 - $finalStatus = proc_get_status($process); - $exitCodeFromStatus = isset($finalStatus['exitcode']) ? $finalStatus['exitcode'] : null; - - file_put_contents($logFile, "[DEBUG] 关闭管道前的最终状态: " . json_encode($finalStatus, JSON_UNESCAPED_UNICODE) . "\n", FILE_APPEND); - file_put_contents($logFile, "[DEBUG] proc_get_status 返回的退出码: " . ($exitCodeFromStatus !== null ? $exitCodeFromStatus : 'null') . "\n", FILE_APPEND); + file_put_contents($logFile, "[DEBUG] 已保存的退出码: " . ($finalExitCode !== null ? $finalExitCode : 'null') . "\n", FILE_APPEND); // 关闭管道 $closeResults = []; @@ -601,15 +597,18 @@ class TaskSchedulerCommand extends Command file_put_contents($logFile, "[DEBUG] proc_close 返回的退出码: {$exitCodeFromClose}\n", FILE_APPEND); file_put_contents($logFile, "[DEBUG] 退出码类型: " . gettype($exitCodeFromClose) . "\n", FILE_APPEND); - // 优先使用 proc_get_status 的退出码,因为 proc_close 在某些情况下会错误返回 -1 - // 如果 proc_get_status 有退出码且进程已结束,使用它;否则使用 proc_close 的返回值 - if ($exitCodeFromStatus !== null && !$finalStatus['running']) { - $exitCode = $exitCodeFromStatus; - file_put_contents($logFile, "[DEBUG] ✅ 使用 proc_get_status 的退出码: {$exitCode}\n", FILE_APPEND); + // 优先使用进程刚结束时保存的退出码(proc_get_status 在进程刚结束时的返回值) + // 因为关闭管道后,proc_get_status 可能会返回 -1,这是 PHP 的已知行为 + if ($finalExitCode !== null) { + $exitCode = $finalExitCode; + file_put_contents($logFile, "[DEBUG] ✅ 使用进程结束时保存的退出码: {$exitCode}\n", FILE_APPEND); if ($exitCodeFromClose === -1 && $exitCode === 0) { - file_put_contents($logFile, "[DEBUG] ℹ️ 注意: proc_close 返回 -1,但 proc_get_status 显示退出码为 0,这是 PHP 的已知行为,任务实际执行成功\n", FILE_APPEND); + file_put_contents($logFile, "[DEBUG] ℹ️ 注意: proc_close 返回 -1,但进程结束时的退出码为 0,这是 PHP 的已知行为,任务实际执行成功\n", FILE_APPEND); + } elseif ($exitCodeFromClose !== -1 && $exitCodeFromClose !== $exitCode) { + file_put_contents($logFile, "[DEBUG] ⚠️ 警告: proc_close 返回 {$exitCodeFromClose},但进程结束时的退出码为 {$exitCode},使用进程结束时的退出码\n", FILE_APPEND); } } else { + // 如果没有保存的退出码,使用 proc_close 的返回值 $exitCode = $exitCodeFromClose; file_put_contents($logFile, "[DEBUG] 使用 proc_close 的退出码: {$exitCode}\n", FILE_APPEND); if ($exitCode === -1) {