209

Can I redirect output to a log file and a background process at the same time?

In other words, can I do something like this?

nohup java -jar myProgram.jar 2>&1 > output.log &

Or, is that not a legal command? Or, do I need to manually move it to the background, like this:

java -jar myProgram.jar 2>$1 > output.log
jobs
[CTRL-Z]
bg 1
1
  • 2
    Have you tried it? What error do you get? Also I'm not sure if you have a typo, or an error in your code. 2>$1 is probably supposed to be 2>&1.
    – phemmer
    May 3, 2013 at 0:10

6 Answers 6

282

One problem with your first command is that you redirect stderr to where stdout is (if you changed the $ to a & as suggested in the comment) and then, you redirected stdout to some log file, but that does not pull along the redirected stderr. You must do it in the other order, first send stdout to where you want it to go, and then send stderr to the address stdout is at

some_cmd > some_file 2>&1 &

and then you could throw the & on to send it to the background. Jobs can be accessed with the jobs command. jobs will show you the running jobs, and number them. You could then talk about the jobs using a % followed by the number like kill %1 or so.

Also, without the & on the end you can suspend the command with Ctrlz, use the bg command to put it in the background and fg to bring it back to the foreground. In combination with the jobs command, this is powerful.

to clarify the above part about the order you write the commands. Suppose stderr is address 1002, stdout is address 1001, and the file is 1008. The command reads left to right, so the first thing it sees in yours is 2>&1 which moves stderr to the address 1001, it then sees > file which moves stdout to 1008, but keeps stderr at 1001. It does not pull everything pointing at 1001 and move it to 1008, but simply references stdout and moves it to the file.
The other way around, it moves stdout to 1008, and then moves stderr to the point that stdout is pointing to, 1008 as well. This way both can point to the single file.

4
  • can't seem to capture the pid after this though with $!
    – chovy
    Dec 2, 2015 at 8:21
  • 23
    Also worth noting: you can use &> file.out to redirect both stdin and stdout to an output file, which cuts down on the possibility of a mistake with putting 2>&1 in the wrong place in your command line.
    – Dan
    Jan 4, 2017 at 21:33
  • 1
    so it's better to use just cmd &> log.txt & ?
    – Freedo
    Apr 18, 2022 at 5:40
  • is this going to overwrite the existing log file? should we be doing >> instead of > to append to a log file?
    – Nikhil VJ
    Mar 1 at 8:05
52
java -jar myProgram.jar &> output.log &

Note that the &> directs both stdout and stderr to output.log

2
  • 7
    A one-line explanation will make the answer complete.
    – anaik
    Jun 7, 2017 at 6:12
  • 2
    @abhisheknaik96 it runs the jar file and redirects both stdin and stderr to output.log and make it background process.
    – P Pang
    Oct 26, 2017 at 6:48
17

Stopping with <Ctrl+Z> and continuing in the background with bg is equivalent to execute with & at the end of the command.

So, for run in the background and redirect output:

java -jar myProgram.jar 2> errorOutput.log > output.log &

If you also need that this command does not die when you leave the terminal, then you should use nohup

4
  • Oh I see. You are saying the appended '&' char is redundant?
    – djangofan
    May 7, 2013 at 20:32
  • I just quote the manpage. Since nohup will execute the command in the background anyway, seems redundant to execute nohup itself in the background
    – RSFalcon7
    May 8, 2013 at 0:31
  • 12
    nohup doesn't execute the command in the background, you have to explicitly append &
    – jlliagre
    Jun 3, 2013 at 22:34
  • 4
    After you moved a process to the background with bg, you can detach it from your session by running disown, which makes that the process doesn't die when you close the terminal.
    – Koen.
    Dec 30, 2016 at 15:09
10

The tee command is pretty prevalent too.

nohup java -jar myProgram.jar | tee output.log &

5

Instead of using nohup you can use screen. You can view the status of the program in real time. you can even log all the output to a file. It is useful when you access the server via ssh where you get logged out due to poor connection or inactivity. After logging in you can continue the work from where you left. refer this and this to know in detail.

0
1
java -jar myProgram.jar &> output.log & disown

By &> you can write both stdout and stderr to a file. Latter & sends this process to the background, and disown makes this task independent from the terminal. Now you can even close the terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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