Home > Software design >  Bash Shell Script :: Send STD-OUT in Real Time?
Bash Shell Script :: Send STD-OUT in Real Time?

Time:03-07

At my job, my team and I (we are five people) use a Java program for everyday work. Rather than require everyone to install Java and run the programs on their laptops, I set up a little VM, put the Java code on that, then built a primitive little webpage as an interface. When a colleague pushes a "RUN CODE" button on the webpage, the webpage runs a simple bash shell script:

#!/bin/bash

cd /path/to/java/code
runJavaCode.exe > $OUTPUTFILE
echo "Finished running the program!  Output is...\n"
echo "$OUTPUTFILE"
echo "End of script."

This is all laughably clunky, but it serves our purposes.

Okay, here's the problem: Lately the Java code has needed longer than two minutes to finish its calculations. Its working properly, I'm not concerned about the correctness of the code. But my colleagues are discovering that their web browsers are timing out while they wait for output from the shell script. The browsers give up before the Java code completes.

So I need some solution to keep the TCP session between browser and script alive to buy more time for the code. But how?

One solution that occurred to me was this: The Java code is continuously printing to STD_OUT as it operates. The shell script collects that output into $OUTPUTFILE, then echo's it only the Java code is finished. But couldn't there be some way for the shell script to feed the Java Code output back to the user in real time? That would solve my problem.

FYI: My VM is Ubuntu 22.04 (Jammy Jellyfish). I'm running Apache2 ver 2.4.52 as the webserver, and my bash version is 5.1.16(1).

CodePudding user response:

Try:

runJavaCode.exe | tee $OUTPUTFILE

NOTES:

  • a copy of the java program output is written to $OUTPUTFILE while the output is also sent to stdout
  • I have no idea how that continual stream of output will be handled by your particular web page (ie, the web page will likely need to be coded in such a way to automagically 'refresh' based on a push of new data - the java program's stdout stream)
  • Related