开发者

Unix shell script to capture Java process errors?

开发者 https://www.devze.com 2023-04-05 14:29 出处:网络
Currently we have a shell script that executes a Java class and redirects the output to a single file using 2>&1. I want to trap the output in the Unix shell after the Java class execution and fin

Currently we have a shell script that executes a Java class and redirects the output to a single file using 2>&1. I want to trap the output in the Unix shell after the Java class execution and find if there are any errors. Can this be done开发者_开发百科 without checking the output file?


One option is to send the error output to a separate file from the standard output; then you only have to look and see whether the error file is empty or not. If it's empty, no errors were reported to stderr (which, sadly, does not guarantee that errors were not reported to stdout, but that's a QoI (quality of implementation) issue for the Java program).

You might also look at the exit status of the Java program. It will likely exit with a zero status on success and may exit with a non-zero status on failure. However, it might report errors to stderr and still exit successfully.

Depending on what the Java process does, you might be able to run it with the output captured in a variable by the shell using $(...) or backticks. You'd then scan the variable (instead of a file) to find out whether anything went wrong.


You can use tee:

myscript.sh 2>&1 | tee logfile

This will print all the output of your script (both stdout and stderr) to stdout and also to the logfile.

0

精彩评论

暂无评论...
验证码 换一张
取 消