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

Java JSch执行命令时Docker Python脚本输出无法捕获问题

问题:JSch执行Docker命令无法捕获Python脚本输出

相关脚本与执行结果

原始Bash脚本(run_transcribe_py.sh)

RESULT=$(/usr/bin/docker run --mount type=bind,source="/usr/src/Projects/docker/transcribe/audio",target=/home/audio --mount type=bind,source="/usr/src/Projects/docker/transcribe",target=/home/transcribe -it --rm --name transcribe transcribe python /home/transcribe/transcribe.py $1)

echo $RESULT

echo "ABC"

Docker中运行的Python脚本

import sys

transcript="abcdef"

print(transcript)

本地终端执行正常输出

# /bin/bash /usr/src/Projects/docker/transcribe/run_transcribe_py.sh /home/audio/102303160955qhhs5zq.mp3
abcdef
ABC
#

JSch执行异常

通过Java JSch的exec命令执行时,仅能捕获到ABC,Python脚本输出的abcdef为空:

# 

ABC
#

对应的Java代码:

public boolean commitTranscription(ArrayList<transcriptionMetaData> pRecordingsInfo) {
        boolean retVal = false;

        JSch localJsch = null;
        localJsch = new JSch();

        Session localSession = initJSch(localJsch, AppSettings.getPbxServer(), AppSettings.getPbxUser(), AppSettings.getPbxServerPassword(), AppSettings.getPbxPort());

        try {
            for (transcriptionMetaData iterateRecData : pRecordingsInfo) {
                ArrayList<String> transcribeLines = new ArrayList<String>();

                ChannelExec shellChannel = (ChannelExec) localSession.openChannel("exec");

                try ( BufferedReader resultOfTranscription = new BufferedReader(new InputStreamReader(shellChannel.getInputStream()))) {
                    shellChannel.setCommand("/bin/bash /usr/src/Projects/docker/transcribe/run_transcribe_py.sh /home/audio/"
                            + iterateRecData.getCallLogNo() + ".mp3");
                    shellChannel.connect((int) TimeUnit.SECONDS.toMillis(10));

                    String resultLine = null;

                    while ((resultLine = resultOfTranscription.readLine()) != null) {
                        transcribeLines.add(resultLine);
                    }

                    iterateRecData.setTranscript(transcribeLines.toString());

                    if (shellChannel != null) {
                        if (shellChannel.isConnected()) {
                            shellChannel.disconnect();
                        }

                        shellChannel = null;
                    }
                }

                transcribeLines = null;
            }
        } catch (JSchException jex) {
            localLogger.error((String) logEntryRefNumLocal.get()
                    + "JSch exception in commitTranscription() method in ExperimentalRecordingsTranscription. JSch exception: " + jex.toString() + ". Contact software support." + jex.getMessage(), jex);
        } catch (RuntimeException rex) {
            localLogger.error((String) logEntryRefNumLocal.get()
                    + "Runtime exception in commitTranscription() method in ExperimentalRecordingsTranscription. Runtime exception: " + rex.toString() + ". Contact software support." + rex.getMessage(), rex);
        } catch (Exception ex) {
            localLogger.error((String) logEntryRefNumLocal.get()
                    + "Exception in commitTranscription() method in ExperimentalRecordingsTranscription. Exception: " + ex.toString() + ". Contact software support." + ex.getMessage(), ex);
        } finally {
            if (localSession != null) {
                if (localSession.isConnected()) {
                    localSession.disconnect();
                }

                localSession = null;
            }

            localJsch = null;
        }

        return retVal;
    }

已尝试的修改

修改了Bash脚本的变量声明和输出方式,但问题依旧:

#!/bin/bash

declare RESULT

RESULT="$(/usr/bin/docker run --mount type=bind,source="/usr/src/Projects/docker/transcribe/audio",target=/home/audio --mount type=bind,source="/usr/src/Projects/docker/transcribe",target=/home/transcribe -it --rm --name transcribe transcribe python /home/transcribe/transcribe.py "$1")"

printf '%s\n' "$RESULT"

echo "ABC"

同时尝试直接在Java中调用Docker命令、跳过Bash脚本,结果相同:Java无法捕获Docker内Python脚本的标准输出,仅能看到ABC,本地终端执行则正常输出abcdef和ABC。

疑问

请问该问题的原因是什么,如何解决?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 17:47:46