2

I've a Java standalone program scheduled to run as cron at every 10 minutes

I want to catch/write errors thrown by this Java program both in the log file and also as a separate error file (MyJavaStandalone.err).

I know the following commands:
Errors redirected to a separate file but not to log file

/usr/java/jdk1.6.0/bin/java MyJavaStandalone >> MyJavaStandalone.log 2>> MyJavaStandalone.err &

Both log and errors are redirected to the same log file, but errors alone are not written to a separate error file

/usr/java/jdk1.6.0/bin/java MyJavaStandalone >> MyJavaStandalone.log 2>&1 &
Gnanam
  • 1,459
  • 13
  • 26
  • 32

2 Answers2

1

Give this a try:

/usr/java/jdk1.6.0/bin/java MyJavaStandalone 2>&1 >> MyJavaStandalone.log | tee -a MyJavaStandalone.err >> MyJavaStandalone.log &
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • @Dennis That's great. It's working perfectly as I expected. By the way, I've a small question, why do we need to specify `MyJavaStandalone.log` twice before and after `tee` command? – Gnanam Apr 01 '10 at 09:55
  • The first one is for receiving the standard output and the second is for receiving the error output as a "branch" of the "tee" (which is connected to the "pipe" - think in terms of plumbing). – Dennis Williamson Apr 01 '10 at 14:12
0

You can achieve what you want like this:

/usr/java/jdk1.6.0/bin/java MyJavaStandalone 3>&1 2>&1 1>MyJavaStandalone.log| tee -a MyJavaStandalone.log > MyJavaStandalone.err

What it does:

  • Create a new file descriptor 3, which will be output to STDOUT (3>&1)
  • Redirect STDERR to STDOUT (2>&1)
  • Output STDOUT to logfile (1>MyJavaStandalone.log)
  • tee takes the STDOUT of the previous command, which is actually STDERR of you Java app, appends the output to the logfile (so the logfile now has both streams) and duplicates the stream to STDOUT
  • Last, that STDOUT is simply redirected to your error log
Prof. Moriarty
  • 870
  • 8
  • 12