Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Can I redirect output to a log file and background a 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 background it like so:

java -jar myProgram.jar 2>$1 > output.log
bg 1
share|improve this question
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. – Patrick May 3 '13 at 0:10
up vote 50 down vote accepted

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.

share|improve this answer
can't seem to capture the pid after this though with $! – chovy Dec 2 '15 at 8:21

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

share|improve this answer
Oh I see. You are saying the appended '&' char is redundant? – djangofan May 7 '13 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 '13 at 0:31
nohup doesn't execute the command in the background, you have to explicitly append & – jlliagre Jun 3 '13 at 22:34
Thanks @jlliagre – RSFalcon7 Jun 5 '13 at 11:21

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.