Is there a way to run a process in the background with both the input and the output redirected? I have a java program that is using the Java scanner with the System.in attached to it for input. So I want to redirect the std input to read from a file instead. I'm not sure if this is possible, to begin with. The following does not work :

$: java Bagels << in.txt &

That just hangs.

I also want to write the output of the program to another file to be consumed by another program running simultaneously. Ideally something like this :

$: java Bagels << in.txt & >> out.txt &
$: python player.py << out.txt & >> in.txt &
  • 1
    Use a single <. That << in.txt does not hang, but is waiting for a here-document terminated by in.txt.
    – user313992
    Oct 28, 2019 at 17:38

2 Answers 2


Your first example should start like this

java Bagels < in.txt

As written this runs Bagels with its stdin coming from the file in.txt¹. If you were to append a single trailing & ampersand, your program would be run in the background, leaving your terminal's command prompt ready for you to type more instructions.

I think your second example could be this, but I'm somewhat confused by your in.txt and out.txt; generally it's not a good idea to feed the output of one program to the input of another AND take the output of the second as input to the first.

java Bagels < in.txt | player.py > out.txt

What this does is attach in.txt to the stdin of the Java program (< FROM a file), attach its stdout output to the stdin input of player.py, and write the stdout output of the Python program to the file out.txt (> TO a file in write only mode with truncation, replace with >> to append, or 1<> to open in read+write mode without truncation).

Finally, to answer the question you wrote in your first sentence "Is there a way to run a process in the background with both the input and the output redirected?", you could put it all together like this

java Bagels < in.txt > out.txt &

¹ here opened in read-only mode, replace with <> or 0<> with ksh93 to open in read+write mode, though its extremely rare to actually need to have stdin in read+write


To make this simple i will give an example with the "cat" command ("cat" simply reads input the writes it) When you type "cat < input", the cat command will take its input from the file "input". However, if you type "cat << end_of_input", you are in a (here-document) state, This means that your shell will read the all lines of input you type (from your keyboard), until you write the line "end_of_input" (most of the time a ctrl_d will work as well), and then will execute that cat command with the input you provided (excluding the line end_of_input of course), and that's why it hangs , because it's waiting for you to type the input

enter image description here

As for redirecting standard input, you simply need to write "command < input_file" For redirecting the output of a command to a file, you have two choices:

1- "cmd > output" (notice there is only one ">"), in this case, if a file with the name "output" already exists, it will be overwritten by the output of your command.

2- "cmd >> output" (notice the two ">>"): If the file "output" exists, append the output of the command to it. As for how to make a command take its input from a previous command's output, you might want to use a "pipe"

as a simple example: "ls | cat" , this will redirect the output of the command "ls" into the input of the command "cat". (The fact that the process is run in the background is irrelevant if the process can be run in the background.)

Hope this helps, good luck.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .