Search code examples
javabashloggingnohup

Nohup appending output and error instead of overwrite


I am using nohup to append the java output and error to the same log file. The problem is it writes an output and then it overwrites the log file for error and output is erased.

The nohup command is

nohup java Daemon 1000 >logs/wrapper.log 2>logs/wrapper.log &

This is the message I want to log in wrapper.log from the Daemon.java

System.out.println("This is output that should go to the file");

System.err.println("This is error that should go to the file");

But only the last message is written in the file. The most reasonable answer is to know how to append the wrapper.log for outputs and errors and not the overwrite

Any ideas

Thanks


Solution

  • The most portable (and my preferred method) is:

    cmd >>logs/wrapper.log 2>&1 &
    

    the >>FD redirect opens with the O_APPEND flag. cello's answer is a bashism (and most kshes and zsh) to redirect both stdout and stderr at once, but doesn't solve the problem of opening in append mode.

    See: http://mywiki.wooledge.org/BashPitfalls#somecmd_2.3E.261_.3Elogfile and associated links.

    EDIT: I see this doesn't actually address the append problem. I'll edit the page. The links are still relevant.