PHP中使用proc_open读取cu命令标准输出时fread挂起的解决方案
问题描述
在使用PHP的proc_open函数执行cu命令时,通过fread读取标准输出可能会遇到进程挂起的问题。这种情况通常发生在cu命令产生大量输出或者输出缓冲区满的情况下。
问题分析
cu命令在执行过程中可能会产生大量的输出数据,当这些数据填满操作系统的管道缓冲区时,如果没有及时读取,就会导致写入阻塞,进而使整个进程挂起。
常见场景
cu命令输出大量日志信息
长时间运行的cu会话
网络延迟导致的输出堆积
解决方案
方案一:使用stream_select实现非阻塞读取
通过stream_select监控文件描述符的可读状态,避免在无数据时阻塞。
function executeCuCommand($command) {
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) {
throw new Exception("无法启动进程");
}
// 设置非阻塞模式
stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);
$output = '';
$error = '';
$start_time = time();
$timeout = 30; // 30秒超时
while (true) {
$read = array($pipes[1], $pipes[2]);
$write = null;
$except = null;
if (stream_select($read, $write, $except, 1) === false) {
break;
}
foreach ($read as $stream) {
if ($stream == $pipes[1]) {
$data = fread($stream, 8192);
if ($data !== false && $data !== '') {
$output .= $data;
}
} elseif ($stream == $pipes[2]) {
$data = fread($stream, 8192);
if ($data !== false && $data !== '') {
$error .= $data;
}
}
}
// 检查进程是否已结束
$status = proc_get_status($process);
if (!$status['running']) {
break;
}
// 超时检查
if (time() - $start_time > $timeout) {
proc_terminate($process);
throw new Exception("命令执行超时");
}
// 如果没有数据可读且进程仍在运行,短暂休眠
if (empty($read)) {
usleep(100000); // 100ms
}
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return array('output' => $output, 'error' => $error);
}方案二:分离stdout和stderr处理
分别处理标准输出和标准错误流,避免相互阻塞。
function executeCuCommandSeparated($command) {
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) {
throw new Exception("无法启动进程");
}
// 设置超时
$timeout = 30;
$start_time = time();
// 分别读取stdout和stderr
$output = '';
$error = '';
while (true) {
// 读取stdout
$stdout_data = fread($pipes[1], 4096);
if ($stdout_data !== false && $stdout_data !== '') {
$output .= $stdout_data;
}
// 读取stderr
$stderr_data = fread($pipes[2], 4096);
if ($stderr_data !== false && $stderr_data !== '') {
$error .= $stderr_data;
}
// 检查是否应该退出循环
$status = proc_get_status($process);
$should_exit = !$status['running'] ||
(feof($pipes[1]) && feof($pipes[2])) ||
(time() - $start_time > $timeout);
if ($should_exit) {
break;
}
usleep(50000); // 50ms
}
// 清理资源
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return array('output' => $output, 'error' => $error);
}方案三:使用命名管道和缓冲控制
对于大量输出的情况,可以使用命名管道来控制缓冲区大小。
function executeCuWithNamedPipes($command) {
$tmp_dir = sys_get_temp_dir();
$stdout_pipe = tempnam($tmp_dir, 'cu_stdout');
$stderr_pipe = tempnam($tmp_dir, 'cu_stderr');
unlink($stdout_pipe);
unlink($stderr_pipe);
posix_mkfifo($stdout_pipe, 0600);
posix_mkfifo($stderr_pipe, 0600);
$wrapped_command = "{$command} > {$stdout_pipe} 2> {$stderr_pipe}";
$pid = pcntl_fork();
if ($pid == 0) {
// 子进程执行命令
shell_exec($wrapped_command);
exit(0);
}
// 父进程读取管道
$output = '';
$error = '';
$stdout_fp = fopen($stdout_pipe, 'r');
$stderr_fp = fopen($stderr_pipe, 'r');
while (true) {
$read = array($stdout_fp, $stderr_fp);
$write = null;
$except = null;
if (stream_select($read, $write, $except, 1) === false) {
break;
}
foreach ($read as $stream) {
if ($stream == $stdout_fp) {
$data = fread($stream, 8192);
if ($data !== false && $data !== '') {
$output .= $data;
}
} elseif ($stream == $stderr_fp) {
$data = fread($stream, 8192);
if ($data !== false && $data !== '') {
$error .= $data;
}
}
}
// 检查子进程是否结束
$result = pcntl_waitpid($pid, $status, WNOHANG);
if ($result == $pid || $result == -1) {
break;
}
}
fclose($stdout_fp);
fclose($stderr_fp);
unlink($stdout_pipe);
unlink($stderr_pipe);
return array('output' => $output, 'error' => $error);
}最佳实践建议
1. 合理设置缓冲区大小
根据实际需求调整读取缓冲区大小,避免内存溢出。
2. 实现超时机制
始终为命令执行设置合理的超时时间,防止无限期挂起。
3. 错误处理
完善错误处理机制,确保在异常情况下能够正确清理资源。
4. 资源管理
及时关闭文件描述符和进程句柄,避免资源泄漏。
总结
解决PHP中proc_open读取cu命令输出时的fread挂起问题,关键在于采用非阻塞IO、分离流处理和适当的缓冲控制策略。方案一提供的stream_select方法是最常用且有效的解决方案,适用于大多数场景。在实际应用中,应根据具体需求和系统环境选择最适合的方案。