Gathering output from a Java app and shell command
I'm writing a shell script that's supposed to do the following.
- run a Java application that produces output - run a shell command that produces output - gather both outputs and send them out in an emailI have control of the source code of all the steps above.
Is there a best practice in gathering output from different sources? 开发者_JAVA百科Should I redirect everything to a single temp file? Should I write different output to different files then concatenate them? What are the pros and cons of each approach?
If you don't worry about using just Java you could use the Runtime
class to execute the shell command withing java (through exec
) command, this will return you a Process
object on which you have either getInputStream
and getOutputStream
so you will then be able to process the output of both the Java program and the shell command inside just one place and do whatever you want (keeping it in memory and directly send it by redirecting the outputstream to the inputstream of what you use to send the mail, with another exec
) or saving it or whatever.
I'd favor using a second wrapper script which
- calls the java program
- calls the shell script
- Captures output to a single file
- reformats that output
- suitable for mailing out Actually
- does the mailing
Assuming you are using a unix shell, mailing/formatting and shell script calls are much simpler from the command line.
精彩评论