I have this bash script "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"
The Python script run in the docker:
import sys
transcript="abcdef"
print(transcript)
If I run the bash script in BASH:
# /bin/bash /usr/src/Projects/docker/transcribe/run_transcribe_py.sh /home/audio/102303160955qhhs5zq.mp3
abcdef
ABC
#
However, if run via a JSCh exec command from 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;
}
Java literally sees
#
ABC
#
The output from the docker Python script (e. g. "abcdef") is just blank in Java, while, if the script is run from BASH itself, both lines are present.
Why is this
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
invisble to Java only, but shows in BASH in the console, just above "abc"?
Anybody got any idea?
Thanks!
EDIT: From feedback from Charles Duffy (thanks Charles!) I've changed my BASH script as referenced above to
#!/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"
This still however, in Java results in the exact same blank output if the BASH script is called from the JsCH exec method:
ABC
while running it straight in BASH results in
abcdef
ABC
I literally just want the "abcdef" to be "visible" to Java in the Java code above... so nothing changes even if I clean up the variable instantiation in BASH and the output of it via printf instead of echo as advised by the link Charles gave...
EDIT: I also tried calling the dockerised Python instance directly from Java and skip BASH alltogether - behaviour remains exactly the same. Java never sees the output printed to stdout by docker from running the Python script inside docker.
E. g.
shellChannel.setCommand("/bin/bash /usr/src/Projects/docker/transcribe/run_transcribe_py.sh /home/audio/" + iterateRecData.getCallLogNo() + ".mp3");
changed to
shellChannel.setCommand("/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 " + iterateRecData.getCallLogNo() + ".mp3");
still gives the same blank result. The Java BufferedReader never sees the output printed to stdout by Python running inside docker. If run from the terminal directly with the above commandline, result is as expected - the letters "abcdef" appears in the terminal.