Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 &
share|improve this question
up vote 1 down vote accepted

Give this a try:

/usr/java/jdk1.6.0/bin/java MyJavaStandalone 2>&1 >> MyJavaStandalone.log | tee -a MyJavaStandalone.err >> MyJavaStandalone.log &
share|improve this answer
@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 1 '10 at 9: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 1 '10 at 14:12

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
share|improve this answer
Both "log" and "err" should keep appending. This command is overwriting both the files, when I execute next time. – Gnanam Apr 1 '10 at 10:09
You're right, for that you need to change the last two '>' to '>>' – Prof. Moriarty Apr 1 '10 at 11:43
You're not making use of file descriptor 3 after you create it. Try removing the 3>&1 and you'll find that it still works. At that point your command is the same as mine. – Dennis Williamson Apr 1 '10 at 14:11
You're right, I realized that on my own after a while :P – Prof. Moriarty Apr 1 '10 at 18:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.