Redirect stdout to a file when process is run in background
Solution 1
What about sh -c './program > file.txt; cat file.txt' &
?
Solution 2
When redirecting stdout to a text file, you are changing the stream type from console to file. The console output is unbuffered, while the file output is buffered. Until the program flushes the stdout device (file), the text will continue to buffer until the buffering threshold is reached. If the text "output\n" is printed every 2 seconds, and the threshold is 4,096 bytes, it would take nearly 20 minutes to see any output from the program in the file.
Rauffle
Updated on September 18, 2022Comments
-
Rauffle over 1 year
How can I redirect the stdout of a program to a file when it's run in the background?
I have a program that generates output every second. When I run it normally and redirect to a file, the output is in that file as expected:
#./program > file.txt #cat file.txt output output output #
When I try to do the same thing in the background, the file will remain empty:
#./program > file.txt & #cat file.txt #
-
slhck almost 12 yearsNot the way I expect it to be. What program are you running?
-
Rauffle almost 12 yearsThe program itself is irrelevant but is a simple executable that checks something every second and writes the current value to stdout.
-
Alex Allen almost 12 yearsIs it also empty when the program is finished?
-
speakr almost 12 years@Rauffle I guess the program itself may be not irrelevant. Normally, you should have the content in your file. Are you sure the program outputs to STDOUT and not e.g. to STDERR?
-
garyjohn almost 12 yearsDoes the
jobs
command show that your program is running? It may be stopped for some reason. -
Rauffle almost 12 yearsTried with another program that prints to stdout and it's the same thing. If I direct stdout to a file but run it in the foreground the file will have content. If I run it in the background, the file remains empty even after the program finishes running. I'm rather new to CentOS, is this perhaps some kind of 'feature' of the OS?
-
chepner almost 12 yearsCan you give us more detail on the actual program you are running? Ordinarily, running in the background should make no difference.
-
billc.cn almost 12 yearsMaybe your program needs to take input from StdIn and crashed because it's not connected?
-
Xiè Jìléi almost 12 yearsWhat about
sh -c './program > file.txt; cat file.txt' &
? -
Rauffle almost 12 years@Xie I've since found a workaround to achieve what I wanted, but this worked (modified to "sh -c './program > file.txt' &"). Want to submit it as an answer?
-
Xiè Jìléi almost 12 years@Rauffle Done! :)
-
-
singpolyma over 8 yearsThis does not work for me
-
singpolyma over 8 yearsstackoverflow.com/a/11337310/8611 with tee worked. Not sure why buffering is different in background than foreground